Get-Clipboard and Out-Clipboard in PSCX

PowerShell Community Extensions (PSCX) has a couple of handy cmdlets for dealing with clipboard text.  Get-Clipboard alias gcb, gets the current clipboard text and sends it down the pipe.  I find this useful when copying PowerShell scripts or other text from the clipboard that I want to quickly place in a file e.g.:
 
> Get-Clipboard > foo.ps1
 
Out-Clipboard alias ocb, is very use for placing PowerShell output onto the clipboard.  Perhaps you want to mail your current process stats to a sysadmin e.g.:
 
> Get-Process | Sort PM -desc | select -f 20 | Out-Clipboard
 
One feature that Out-Clipboard provides is that it allows you to specify the formatting output width.  For instance if you have text that is wrapping on your 120 character width console, then you can specify a larger width like so:
 
> gci "$Home\My Documents\My Music" -rec | %{$_.Fullname} | ocb -width 260
 
This will create output that doesn’t wrap.  Note also that by default Out-Clipboard trims whitespace at the end of each line.  If for some reason you don’t want that behavior you can use the -NoTrimEnd option.
 
Where things get really interesting is to use PowerShell to massage clipboard text before you paste it somewhere else.  I use this example fairly often:
 
> gcb | %{$_ -replace ‘\t’, ‘    ‘} | ocb
 
This little PowerShell command line gets the text on the clipboard and replaces all the tabs with four spaces and then places the modified text back on the clipboard.
Advertisements
This entry was posted in PSCX. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s