Sending Text to Clipboard Everywhere

In a previous tip you learned how to use clip.exe to send results to the clipboard. But what if you don't have clip.exe (let's say on Windows XP) or don't want dependencies?

Here's a clever alternative:

function Out-Clipboard {
 param(
  $text
 )
 Add-Type -AssemblyName System.Windows.Forms 
 $tb = New-Object System.Windows.Forms.TextBox 
 $tb.Multiline = $true 
    
 if ($Input -ne $null) {
  $Input.Reset()
  $tb.Text = $Input | Out-String
 } else {
  $tb.Text = $text 
 }
 $tb.SelectAll() 
 $tb.Copy() 
}

Use it like this:

PS> Get-Process | Out-Clipboard

It solely uses .NET Framework functionality that is available in all versions and modes of PowerShell.

Twitter This Tip! ReTweet this Tip!


Posted Jan 03 2012, 06:00 PM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.