How To Copy Paste Content via Clipboard in PowerShell

0

We use copy/paste almost every day through many of the applications we use. I even use Win+V a lot more since Windows 10 introduced the new clipboard history feature. But how do we deal with the same thing in a PowerShell environment, such as send a result straight to the clipboard?

While the clip trick still works, there is a much better way with the help from two native PowerShell cmdlets, Set-Clipboard and Get-Clipboard.

Set-Clipboard

The cmdlet Set-Clipboard is the replacement of clip.exe but behaves the same way and more. You can still pipe the output to send the result to the clipboard.

PS> $env:ComputerName | Set-Clipboard

Since Set-Clipboard has an Append switch, you can add more content to the current clipboard without removing the previous ones.

PS> $env:SystemRoot | Set-Clipboard -Append

Other than piping the output to the cmdlet, you can also directly use the cmdlet to save a string to the clipboard.

PS> Set-Clipboard -Value "Sending a string of text to the clipboard"

Or directly copy any of the items to the clipboard by specifying its path.

PS> Set-Clipboard -Path "h:\scripts"

Get-Clipboard

Now let’s see how to get the content out of the clipboard using the cmdlet Get-Clipboard.

Since there are different types of contents being copied into the clipboard, we will need to use different format parameters to differentiate them.

See the last sample that copies the folder into the clipboard? You will need to use the FileDropList format to retrieve it from the clipboard.

PS> Get-Clipboard -format FileDropList
image - How To Copy Paste Content via Clipboard in PowerShell

What about an image? Use the Image format.

PS> Get-Clipboard -format image
image 1 600x263 - How To Copy Paste Content via Clipboard in PowerShell

This way, you can retrieve various properties about the image.

How about empty out the clipboard?

Unfortunately, since Set-Clipboard doesn’t take the null value, we are still stuck in the old fashion way.

cmd /c "echo off | clip"

Last note, both Set-Clipboard and Get-Clipboard cmdlets are available in PowerShell 5.0 but may not be available in PowerShell Core or Version 6, unfortunately.

SHARE

LEAVE A REPLY

Please enter your comment!
Please enter your name here