Using PowerShell Copy to Clipboard Function

Published:1 August 2019 - 3 min. read

When writing code in PowerShell the last thing you might think about is
the Windows clipboard. We all probably know the infamous Ctrl-C and Ctrl-V keyboard shortcuts to copy and paste from the clipboard but this is typically done via the GUI. In our PowerShell scripts, it’s not too useful. However, did you know that there is PowerShell copy to clipboard function?

In this article, I’m going to go over how we used to get copy and paste functionality in PowerShell and then introduce you to the cmdlets that PowerShell provides starting in version 5.

Prior to PowerShell v5, the only way to copy to the clipboard was by using the clip.exe application.

Old School clip.exe

This application had been around a long time and just worked. By piping text to clip.exe, you could get text copied onto the clipboard. This worked but it was always annoying when it added a carriage return after each entry.

PS51> Write-Output 'Hello' | clip
Copying to clipboard with clip.exe
The PowerShell copy to clipboard function can replace the old school clip.exe

Not only that but we had no easy way to pull information from the clipboard. We were forced to manually paste the contents once they were in the clipboard. With the native clipboard commands, this is no more!

We now have two cmdlets that solve this problem much better; Get-Clipboard and Set-Clipboard.

Set-Clipboard Powershell Command

Set-Clipboard replaces clip.exe but behaves exactly the same. Now, instead of piping output to clip, you can use Set-Clipboard. If you do this, you’ll notice that the trailing carriage return is gone when you paste the contents somewhere.

The clipboard now only contains the string Hello.

PS> Write-Output 'Hello' | Set-Clipboard

Another useful feature with Set-Clipboard is the Append parameter. Traditionally, the clipboard has only been able to store one item. Now, by using the Append parameter with Set-Clipboard, you can add an unlimited number of items without removing the previous.

PS> Write-Output 'Hello' | Set-Clipboard -Append

Get-Clipboard Parameters

Even if you don’t use Set-Clipboard to copy contents to the clipboard, you can still use Get-Clipboard to understand a few different types other than simple strings. For example, if you’ve copied and a list of files from File Explorer and you’d like to get this list into your PowerShell console, simply use the Format parameter and the �FileDropList value on Get-Clipboard.

PS51> Get-Clipboard -Format FileDropList

Directory: \\Mac\Home\Downloads

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------         8/1/2019  10:42 AM       17919094

    Directory: \\Mac\Home\Documents\Snagit\Autosaved Captures.localized

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------         8/1/2019   1:58 PM      169144472 2019-08-01_13-47-55.mp4

You can see that it understands the type of data in the clipboard is a list of files. Also, you can do the same with images and other entities.

Copy an image from another application and use provide -Format Image to Get-Clipboard to retrieve various properties about the image.

PS51> Get-Clipboard -Format Image

Tag                  :
PhysicalDimension    : {Width=813, Height=113}
Size                 : {Width=813, Height=113}
Width                : 813
Height               : 113
HorizontalResolution : 96
VerticalResolution   : 96
Flags                : 335888
RawFormat            : [ImageFormat: b96b3caa-0728-11d3-9d7b-0000f81ef32e]
PixelFormat          : Format32bppRgb
Palette              : System.Drawing.Imaging.ColorPalette
FrameDimensionsList  : {7462dc86-6180-4c7e-8e3f-ee7333a7a483}
PropertyIdList       : {}
PropertyItems        : {}

Clipboard Manipulation in PowerShell Core

Unfortunately, the Get-Clipboard and Set-Clipboard commands are not available in PowerShell Core (v6+) but you can still use them using the WindowsCompatibility module. The WindowsCompatibility module allows you to use Windows PowerShell cmdlets within PowerShell Core.

Once you have the WindowsCompatibility module installed, run Invoke-WinCommand and wrap your Get-Clipboard and Set-Clipboard references inside.

PS622> Invoke-WinCommand -ScriptBlock {'This is on the clipboard' | Set-ClipBoard}
PS622> Invoke-WinCommand -ScriptBlock {Get-ClipBoard}
This is on the clipboard

You’ll find that you can read and write to the clipboard just as you would with Windows PowerShell.


You can do so much more with the clipboard in PowerShell nowadays. The PowerShell copy to clipboard has been made possible using the Get-Clipboard and Set-Clipboard. These cmdlets allows you to use PowerShell to copy to the clipboard and to intelligently paste from the clipboard too.

For a full breakdown of the cmdlets, check out the detailed help:

PS51> Get-Help Set-Clipboard -Detailed
PS51> Get-Help Get-Clipboard -Detailed

This will give you all of the various parameters that you can try out.

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!