Using the Clipboard (Copy/Paste) in PowerShell

PowerADM.com / PowerShell / Using the Clipboard (Copy/Paste) in PowerShell

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

PowerShell: Set-Clipboard

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

Copy and paste files to clipboard in windows with powershell

The following FileDropList attributes can be used to obtain information about different file types in the clipboard:

  • Text – by default
  • FileDropList – get information about files in the clipboard
  • Image – get image file metadata
  • Audio – 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.

Get the type of data in clipboard?

You can clear the clipboard with the command:

[Windows.Clipboard]::Clear()
Leave a Reply

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