Condividi tramite


PowerShell Basics: Copying The Output Of The Last PowerShell Command To Clipboard

I recently needed to copy and paste a PowerShell script output. While poking around in PowerShell, I discovered that both trying to copy and paste it out of PowerShell or hitting the up arrow and piping whatever the last command was into Set-Clipboard was such a hassle!

 

So to address this, I threw the following small function into my profile:

 function cc { r | scb }

 

Now PowerShell 5.0 is needed to evoke this. Specifically for use of Set-Clipboard. Lets now break down the workings of the small function.

First I’m defining a function named cc which is not a properly named PowerShell function but for now it will do the trick. Its assigned r | scb in which r is an alias for Invoke-History which re-runs the last command typed. Try it yourself:

 

 PS G:\> write-output "hah!"
hah!

PS G:\> r
write-output "hah!"
hah!

PS G:\> r
write-output "hah!"
hah!

 

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

 

PowerShell_Tips_Tricks

Comments

  • Anonymous
    October 11, 2016
    Good one!! Thanks for sharing!