Note that there are some explanatory texts on larger screens.

plurals
  1. POPipe output to the clipboard using PowerShell
    primarykey
    data
    text
    <p>In PowerShell, how do you pipe the output of a command to the clipboard but</p> <ul> <li>Still be able to pipe the data to more processes</li> <li>No dependencies on external applications like <code>clip.exe</code></li> <li>for it to work as a filter, so we see output on the command line immediately</li> </ul> <p>?</p> <h1><strong>EDIT: 14 May 2015</strong></h1> <p>After 3 years, I thought I would share my <code>ClipboardModule</code> (I hope I am allowed to):</p> <pre><code>Add-Type -AssemblyName System.Windows.Forms Function Get-Clipboard { param([switch]$SplitLines) $text = [Windows.Forms.Clipboard]::GetText(); if ($SplitLines) { $xs = $text -split [Environment]::NewLine if ($xs.Length -gt 1 -and -not($xs[-1])) { $xs[0..($xs.Length - 2)] } else { $xs } } else { $text } } function Set-Clipboard { $in = @($input) $out = if ($in.Length -eq 1 -and $in[0] -is [string]) { $in[0] } else { $in | Out-String } if ($out) { [Windows.Forms.Clipboard]::SetText($out); } else { # input is nothing, therefore clear the clipboard [Windows.Forms.Clipboard]::Clear(); } } function GetSet-Clipboard { param([switch]$SplitLines, [Parameter(ValueFromPipeLine=$true)]$ObjectSet) if ($input) { $ObjectSet = $input; } if ($ObjectSet) { $ObjectSet | Set-Clipboard } else { Get-Clipboard -SplitLines:$SplitLines } } Set-Alias cb GetSet-Clipboard Export-ModuleMember -Function *-* -Alias * </code></pre> <p>I usually use the <code>cb</code> alias (for <code>GetSet-Clipboard</code>) because it is two way i.e can get or set the clipboard:</p> <pre><code>cb # gets the contents of the clipboard "john" | cb # sets the clipboard to "john" cb -s # gets the clipboard and splits it into lines </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload