Windows PowerShell 5.1 introduced two built-in cmdlets that allow you to copy and paste text and/or files from the Windows Clipboard from the PowerShell command line.
In order to copy text data to the clipboard, execute the following command
Set-Clipboard -Value "Test clipboard"
or use a pipe:
Get-Process| Set-Clipboard
To list (paste) the current contents of the clipboard to the command prompt console:
Get-Clipboard
You can use PowerShell to add data to the clipboard without overwriting its contents. To do this, use the Set-Clipboard command with the -Append parameter:
Set-Clipboard -Value " add new text" -Append
You can copy files to the clipboard as well as text data. In the following example, we will copy all the files in a specified directory to the clipboard:
Get-ChildItem "c:\ps" | Set-Clipboard
You can view information about files copied to the clipboard:
Get-Clipboard -Format FileDropList
The following FileDropList attributes can be used to obtain information about different file types in the clipboard:
Text
– by defaultFileDropList
– get information about files in the clipboardImage
– get image file metadataAudio
– get info about an audio file
In order to empty the clipboard, you can use the following command
Set-Clipboard -Value $null
In earlier versions of PowerShell (before 5.1), you can copy data to the clipboard by using the following command:
$myVar = Get-Process
$myVar | clip.exe
To get the contents of the clipboard:
Add-Type -AssemblyName PresentationCore
[Windows.Clipboard]::GetText()
You can check whether the clipboard contains text data or files:
[Windows.Clipboard]::ContainsText()
[Windows.Clipboard]::ContainsFileDropList()
If the clipboard contains only text, the first command returns True and the second returns False.
You can clear the clipboard with the command:
[Windows.Clipboard]::Clear()