Saving PowerShell Results to the Clipboard–#SQLNewBlogger

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

Working with results and manipulating them is always something I struggle with a bit in PowerShell. I’ve slowly been learning how to limit results to what I want and then save those out.

Recently, I was reading Learn dbatools in a Month of Lunches, and I learned something new. I can save results to the clipboard easily. Here’s an example:

Let’s say I want to get backups status. I’d run something like this, using the Get-DbaDbBackupHistory cmdlet:

2021-12-07 09_44_04-C__Users_Steve

I see results, but I might want to put this into a report or email that I send to others. I can certainly highlight this in the cmdline and copy it, but there’s an easier way.

I can pipe this to clip, and then I don’t see the results.

2021-12-07 09_44_08-C__Users_Steve

Then I can CTRL+V and paste this into notepad (or anywhere).

2021-12-07 09_44_12-_Untitled - Notepad

Super handy way to take information from the PoSh cmdline and ensure I get everything. Miss-highlighting when not paying attention has caused me problems before, especially when the results scroll off the screen. Highlighting a lot of information is hard, so using clip is a good trick.

SQLNewBlogger

This was a quick trick I learned while doing something else, so I decided to write a quick post. This took me about 5 minutes to put together.

You could do the same thing, showing how you’d use this in your job.

About way0utwest

Editor, SQLServerCentral
This entry was posted in Blog and tagged , , . Bookmark the permalink.

7 Responses to Saving PowerShell Results to the Clipboard–#SQLNewBlogger

  1. JeffModen says:

    Thanks for the post, Steve.

    As a bit of feedback and I realize that the backup history isn’t the crux of this thread, we can’t see parts of the commands because of the very low intensity dark mode that you’ve selected.

    As a bit of a sidebar, this reminds of an old saying with a new word in it… 😀

    “Just because you CAN do something in PowerShell, doesn’t mean you should”. 😀

    Like

    • way0utwest says:

      I’ll try to remember the contrast and ensure the code is available.

      This is a great way to get the data quickly and easily, much easier than I’d see in T-SQL, where I’d have to dig into docs to get some.

      Like

    • JeffModen says:

      There will always be that argument, I suppose. I shouldn’t have brought it up on this thread because it detracts from the thrusts of these great posts. For me, those thrusts (especially for but not limited to new bloggers/presenters, ect) is to keep learning new things, it doesn’t take long to build up a library of tools, and you can really help your career by blogging and even writing short “lightning” post and articles.

      And thanks for everything else you do, Steve.

      Like

  2. Loyaltyhub says:

    Thanks for the post, Steve.

    As a bit of feedback and I realize that the backup history isn’t the crux of this thread, we can’t see parts of the commands because of the very low intensity dark mode that you’ve selected.

    https://cnvloyalty.com/

    Like

  3. JeffModen says:

    Heh… spammers have no respect for anything. Lordy. That company is now one of the many companies that I will never do business with because of junk like what they just pulled in the post above.

    Like

  4. brianary says:

    Something I miss in PowerShell Core (6+) that was very handy in Windows PowerShell (1-5.x) is the -AsHtml parameter for Set-Clipboard.

    ps power* |select Name,Id,FileVersion,HandleCount |ConvertTo-Html -Fragment |Set-Clipboard -AsHtml

    This allows you to paste formatted tables into emails, chat apps that support formatting (like Teams), documents, or whatever.

    Maybe they’ll add it back, but it doesn’t seem like a priority: https://github.com/PowerShell/PowerShell/issues/14758

    Like

  5. JeffModen says:

    It’s only fair that they removed it. 😀 I used to love what sp_MakeWebTask did and they removed that in 2005 because of “security issues”.. The replacement? Why SSRS, of course 😦

    I rewrote my morning job reports to use XML functionality to build the formatted messages in the emails it would send me each morning. It wasn’t difficult….. just shouldn’t have been necessary to do. Using SSRS seem comparatively complex to me but that’s just my opinion.

    Like

Comments are closed.