Skip to content

Instantly share code, notes, and snippets.

@stej
Created September 21, 2011 18:28
Show Gist options
  • Save stej/1232891 to your computer and use it in GitHub Desktop.
Save stej/1232891 to your computer and use it in GitHub Desktop.
clip
## samples:
#1. copy this gist to clipboard (via CTRL+C); the following script will filter only rows that contain 'function' a copy them back to clipboard
(clip -AsLines) -match 'deleted' | clip
# I use it quite often when looking for some info in log files
#2. Quite useful when working with conversion functions. Let's define conversion to base64.
function ToBase64($str) {
[system.convert]::ToBase64String([system.text.encoding]::utf8.getBytes($str))
}
# then just copy a text to clipboard and call
ToBase64 (clip) | clip
# converted string is in your clipboard
## code:
function Set-ClipBoard {
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0)][object]$s
)
begin { $sb = new-object Text.StringBuilder }
process {
$s | % {
if ($sb.Length -gt 0) { $null = $sb.AppendLine(); }
$null = $sb.Append($_)
}
}
end { [windows.forms.clipboard]::SetText($sb.Tostring()) }
}
function Get-ClipBoard {
[windows.forms.clipboard]::GetText()
}
function clip {
param(
[Parameter(Mandatory=$false,ValueFromPipeline=$true)][object]$s,
[Parameter()][switch]$AsLines
)
begin { $sb = new-object Text.StringBuilder }
process {
$s | % {
if ($sb.Length -gt 0) { $null = $sb.AppendLine(); }
$null = $sb.Append($_)
}
}
end {
if ($sb.Length -gt 0) { $sb.Tostring() | Set-ClipBoard}
else {
$c = Get-ClipBoard
if ($AsLines) { $c -split [environment]::NewLine }
else { $c }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment