Get-Clipboard and Set-Clipboard Cmdlets

In earlier betas of PowerShell there was an implementation of get-clipboard and set-clipboard in the .NET 2.0 RTM drop those cmdlets were removed.  Bummer.  Ever since I discovered wcopy/wpaste in MKS Toolkit I’ve been a big fan on having a way to easily copy/paste command line stuff to/from the clipboard.  So in my efforts to learn how to write PowerShell cmdlets I have created my own Get-Clipboard and Set-Clipboard.  The code is here.  A couple of things to note about this cmdlet.  I recall hearing somewhere that PS runs in the multi-threaded apartment. Apparently this can cause problems when using the WinForms Clipboard class as it expects single-thread apartment. To work around this problem I spin up a worker thread in an STA to do the actual get/set of the clipboard.  The second thing to note is usage.  If you compile the code and create a new shell and try it out you might notice the following:
[C:\Program Files\Microsoft Command Shell\Examples]
PS:29 # dir | set-clipboard


Results in the following copied to the clipboard:
This is a by-product of the way PS works.  While objects (one FileInfo in this case) are passing thru the pipeline they don’t have any formatting applied to them until they reach the end of the pipeline.  But in this case, the set-clipboard cmdlet is at the end of the pipeline.  There is a way to get the "formatted" output onto the clipboard:
[C:\Program Files\Microsoft Command Shell\Examples]
PS:30 # dir | out-string | set-clipboard
That commands results in the following being copied to the clipboard:
    Directory: FileSystem::C:\Program Files\Microsoft Command Shell\Examples
Mode                LastWriteTime     Length Name                             
—-                ————-     —— —-                             
-ar–        10/12/2005   9:59 PM       1895 profile.msh                      
Note that out-string faithfully represents what would have appeared on the console host screen including truncation.  This isn’t something you typically want with text copied to the clipboard so I would advise using the -width parameter if you run into truncation e.g.:
[C:\Program Files\Microsoft Command Shell\Examples]
PS:31 # dir | out-string -w 120 | set-clipboard
You don’t want to go too big on the width because PowerShell right pads every line with spaces up to the width you specify.  What I would really like to see is a -Auto and perhaps a -NoPad option on Out-String.
This entry was posted in PowerShell. Bookmark the permalink.

3 Responses to Get-Clipboard and Set-Clipboard Cmdlets

  1. Chris says:

    That\’s cool, Keith. I went the other way, though, because I couldn\’t figure out how to write the set-clipboard cmdlet (I\’m still new : ) — I built a format-clipboard console in .NET that I stick at the end of my pipe and monad formats before it gets there:
    // format-clipboard.cs
    class FormatClipboard {  [STAThread]  static void Main() {    Clipboard.SetText(Console.In.ReadToEnd());    }}

  2. Keith says:

    Hey that works too.  🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s