Monday, January 9, 2012

Sending Text to Clipboard Everywhere using Powershell

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

No comments:

Post a Comment