Copy and Paste with PowerShell

There are situations that I want use PowerShell to manipulate some text, something like PowerShell output or from within a text editor. The “clip” utility is a tool that has built into Windows, which allows you to redirect the command output to the Windows system clipboard.

The following PowerShell commands are simple and quick examples of how to pipe output text of a PowerShell command to the clipboard. You can use this method to output text from a file or from another PowerShell command.

There is no standard cmdlet for output to the clipboard:

Windows have native utility named “clip.exe” for copy text to clipboard. With PowerShell you can make a alias for it, for example Out-Clipboard:

Now you can now output for example output ls, like:

The issue is that PowerShell doesn’t expose a way to paste text from the clipboard. However, the WinForms API of the .NET Framework has a GetText() static method on the System.Windows.Forms.Clipboard class.

The function below will paste the text from the clipboard into a non-visual instance of the System.Windows.Forms.Textbox control and then emit the text of the control to the console.

Create a new alias “paste” for this function:

Here some examples to illustrate the clip and paste

In this article I’ll show you how to easy it is to create you own copy and paste commands with PowerShell. Also I show you to create your own command using the New-Alias cmdlet. Since PowerShell 5.0 there are now the Get-Clipboard and Set-Clipboard cmdlets but the paste function is still missing.

I really like to hear what you have to say about this article:

  • Was this article helpful for you or do you have any questions?
  • Do you have additions, suggestions or any other ideas?
  • Just let me know by leaving a comment below.

Thank you for spending some time at my site and in my blog. I hope you come to visit again soon!

About Lex van der Horst 201 Articles
Techwire

Be the first to comment

Leave a Reply