Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.