Set Clipboard

by Jun 28, 2011

If you are using Windows Vista or better, you can pipe text to clip.exe  to copy it to your clipboard:

Dir $env:windir | clip

 

Here is another approach that you can use if your PowerShell host uses the STA mode:

function Set-Clipboard {
  param( $text )
  if ($Host.Runspace.ApartmentState -eq 'STA') {
        Add-Type -Assembly PresentationCore
        [Windows.Clipboard]::SetText($text)
    } else {
    Write-Warning ('Run {0} with the -STA parameter to use this function' -f $Host.Name)
  }
}

Set-Clipboard "Hello World"

 

Twitter This Tip!
ReTweet this Tip!