Skip to content

Instantly share code, notes, and snippets.

@AKuederle
Last active August 29, 2015 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AKuederle/e9b120f505948a9340b2 to your computer and use it in GitHub Desktop.
Save AKuederle/e9b120f505948a9340b2 to your computer and use it in GitHub Desktop.
Functions to manipulate the clipboard from the PowerShell
function read-clipboard
{
PowerShell -NoProfile -STA -Command {
Add-Type -Assembly PresentationCore
[Windows.Clipboard]::GetText()
}
}
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