Skip to content

Instantly share code, notes, and snippets.

@Trucido
Last active March 8, 2019 18:28
Show Gist options
  • Save Trucido/c0f0326f23f59a3677bcb0ea8959d2a2 to your computer and use it in GitHub Desktop.
Save Trucido/c0f0326f23f59a3677bcb0ea8959d2a2 to your computer and use it in GitHub Desktop.
<#
Get-Clipboard.ps1 - Gets clipboard content if content type is text.
Note: Conflicts/replaces Pscx\Get-Clipboard which doesn't work in powershell >= 6
#>
function Get-ClipboardText([switch][Alias("S*","L*")]$SplitLF) {
Set-StrictMode -Version 'Latest'
if($SplitLF) {
# Note: Newline CRLF/CR/LF handling should be automatic, but just in case...
$cmd = {
Add-Type -Assembly PresentationCore
$content = [Windows.Clipboard]::GetText()
if ($content) {
$content -replace "`r", '' -split "`n"
}
}
} else {
$cmd = {
Add-Type -Assembly PresentationCore
$content = [Windows.Clipboard]::GetText()
if ($content) {
$content
}
}
}
if([threading.thread]::CurrentThread.GetApartmentState() -eq 'MTA' -or $PSEdition -eq 'Core') {
& powershell -Sta -NoProfile -NonInteractive -Command $cmd
} else {
& $cmd
}
}
if (!($MyInvocation.InvocationName -eq '.' -or $MyInvocation.Line -eq ''))
{
Get-ClipboardText @args
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment