Getting the clipboard contents using PowerShell

Posted: (EET/GMT+2)

 

I recently needed to do some PowerShell scripting, and one task required me to work with the Windows operating system global clipboard. Surely, the interactive PowerShell command prompt supports the clipboard, but how would you access the contents from your .PS scripts?

Turns out there's a PowerShell command called Get-Clipboard, which does the trick. This command is part of the PowerShell Management 5.0 module, which means you need to have PowerShell version 5 (as in Windows 10) to execute it:

Get-Clipboard

If you don't have the correct module and/or PowerShell version, you will get the following error message:

get-clipboard : The term 'get-clipboard' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path
is correct and try again.

Hope this helps!

Oh, in case you need to store something to the clipboard, remember both the Set-Clipboard cmdlet and the "clip.exe" utility!