Quick Tip: Copy The Output Of The Last PowerShell Command To Clipboard

I recently found myself poking around in PowerShell and going “oh, good now I want to copy and paste that output into an email/dialog box/tweet/notepad/another script/complaint box” and either trying to copy and paste it out of PowerShell or hitting the up arrow and piping whatever the last command was into Set-Clipboard. What a hassle.

So, I threw this small function into my profile.

You’ll need PowerShell 5.0 for this one (for Set-Clipboard). This just looks like gibberish though, what’s going on?

Well, clearly I’m defining a function named cc which is not a properly named PowerShell function but I’m being lazy. What does it do? Well it does r | scb.

r is an alias for Invoke-History which re-runs the last command you typed. Try it yourself.

scb is an alias for Set-Clipboard which means whatever came out of the last command will be the new contents of your clipboard.

The cool thing about this is it doesn’t just have to be text. Check out my other post about all the things Set-Clipboard can do.

0 thoughts on “Quick Tip: Copy The Output Of The Last PowerShell Command To Clipboard

  1. Just stumbled across this blog today and have found it informative!

    Just thought I’d add something for those of us stuck on older versions of Powershell. You can pipe content to the clipboard in previous versions of Powershell by using clip:

    Write-Output “www.workingsysadmin.com” | clip

    1. Totally. And the PowerShell Community Extension module has similar functionality. I like the 5.0 behavior since it supports copying files to the clipboard. Piping to clip.exe is good for downlevel support though 🙂

  2. Bear in mind that the output of a repeated command *might* be different if the previous command modified something – get-mailbox | delete-mailbox would return a lot different result the next time you ran it 😉
    What is the difference between set-clipboard and | clip which is available in previous powershell versions?

    I actually have my powershell profile set to autostart a transcript when launched so I always have the output available (although i’d have to open in notepad to obtain it) but that way I don’t have to worry about the screen buffer not being enough to capture the data needed

    1. Yup, I mostly only use this in situations where I’ve done a Get-* command or something regex-y and non-impactful. You’re totally right that you’re going to want to think twice before running some commands a second time.

      The difference between set-clipboard and clip.exe is that set-clipboard is a new cmdlet included with PowerShell 5.0 and supports a bunch of different features that were never available in vanilla PowerShell before. Check out my other post on this: http://www.workingsysadmin.com/new-stuff-get-clipboard-and-set-clipboard-new-in-powershell-5-0/. Clip.exe just receives text output and is an executable that’s been a part of Windows for a long long time.

Leave a Reply

Your email address will not be published. Required fields are marked *