#PSTip Clear clipboard content

Note: This tip requires PowerShell 2.0 or above.

Have you ever wondered how to clear the contents of the clipboard? There are certainly many ways to do that in PowerShell. Let us see a couple of them.

echo $null | clip.exe

The above snippet is the most trivial. Then, we can use the .NET way of doing it.

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Clipboard]::Clear()

The above snippet needs the Windows Forms namespace to clear the clipboard. Note that you don’t need to add the assembly if you are running the above snippet in PowerShell ISE. We can also use the native Win32 API – EmptyClipboard. This is slightly complex and requires either Pinvoke or .NET Interop.

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: