Last active
August 29, 2015 14:14
-
-
Save AKuederle/e9b120f505948a9340b2 to your computer and use it in GitHub Desktop.
Functions to manipulate the clipboard from the PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function read-clipboard | |
{ | |
PowerShell -NoProfile -STA -Command { | |
Add-Type -Assembly PresentationCore | |
[Windows.Clipboard]::GetText() | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function set-clipboard | |
{ | |
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) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment