# encoding: ascii # api: powershell # title: Set-Clipboard.ps1 # description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes # version: 0.1 # type: script # author: Lee Holmes # license: CC0 # x-poshcode-id: 2219 # x-archived: 2016-07-20T17:09:29 # x-published: 2011-09-09T21:42:00 # # ############################################################################# ## ## Set-Clipboard ## ## From Windows PowerShell Cookbook (O'Reilly) ## by Lee Holmes (http://www.leeholmes.com/guide) ## ############################################################################## <# .SYNOPSIS Sends the given input to the Windows clipboard. .EXAMPLE dir | Set-Clipboard This example sends the view of a directory listing to the clipboard .EXAMPLE Set-Clipboard "Hello World" This example sets the clipboard to the string, "Hello World". #> param( ## The input to send to the clipboard [Parameter(ValueFromPipeline = $true)] [object[]] $InputObject ) begin { Set-StrictMode -Version Latest $objectsToProcess = @() } process { ## Collect everything sent to the script either through ## pipeline input, or direct input. $objectsToProcess += $inputObject } end { ## Launch a new instance of PowerShell in STA mode. ## This lets us interact with the Windows clipboard. $objectsToProcess | PowerShell -NoProfile -STA -Command { Add-Type -Assembly PresentationCore ## Convert the input objects to a string representation $clipText = ($input | Out-String -Stream) -join "`r`n" ## And finally set the clipboard text [Windows.Clipboard]::SetText($clipText) } }