Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe output to the clipboard using PowerShell

Tags:

EDIT: 23 Oct 2020

See postanote's answer.

EDIT: 14 May 2015

After 3 years, I thought I would share my ClipboardModule (I hope I am allowed to):

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 * 

I usually use the cb alias (for GetSet-Clipboard) because it is two way i.e can get or set the clipboard:

cb                # gets the contents of the clipboard "john" | cb       # sets the clipboard to "john" cb -s             # gets the clipboard and splits it into lines 
like image 734
Tahir Hassan Avatar asked Oct 29 '12 18:10

Tahir Hassan


People also ask

How do I copy output in PowerShell?

Use QuickEdit to copy text—Although it's not obvious, the PowerShell command shell lets you select and quickly copy any text displayed in the command shell. Use the mouse to select the text to be copied, then press Enter or right-click on the selected text to copy it to the clipboard.

How do I get to the PowerShell clipboard?

In Windows PowerShell, if you've used File Explorer to copy a directory to the clipboard (using the regular Copy shortcut-menu command or the Ctrl+C keyboard shortcut), you can access it as System. IO. DirectoryInfo instance by passing -Format FileDropList to Get-Clipboard .

How do you pipe the output of a command to a file in PowerShell?

There are two PowerShell operators you can use to redirect output: > and >> . The > operator is equivalent to Out-File while >> is equivalent to Out-File -Append . The redirection operators have other uses like redirecting error or verbose output streams.

How do I use the pipe command in PowerShell?

The `|` character in between the two commands is the “pipe” which indicates that instead of displaying the output of the Get-Content command in the PowerShell command window, it should instead pass that data to the next script (the Measure-Object cmdlet).


2 Answers

If you have WMF 5.0, PowerShell contains two new cmdlets:

get-clipboard and set-clipboard

like image 120
Mark Minasi Avatar answered Sep 20 '22 03:09

Mark Minasi


EDIT: Please look at question instead for solution.

Here is my solution:

Add-Type -AssemblyName 'System.Windows.Forms'  filter Set-Clipboard {     begin {         $cp = @()     }     process {         $_ | Tee-Object -Variable 'cp0'         $cp = $cp + @($cp0);     }     end {         $str = ($cp | Out-String).ToString();          [Windows.Forms.Clipboard]::Clear();          if ( ($str -ne $null) -and ($str -ne '') ) {             [Windows.Forms.Clipboard]::SetText( $str )         }          $cp = @()     } } 

This collects all the objects in an array, $cp. We use Tee-Object to redirect the current element, $_, to both the next process and to store it in the array, $cp. Lastly, once the process is finished we set the clipboard's text.

I have used it in the following way:

dir -Recurse | Set-Clipboard | Select 'Name' 

And it seems to work.

To use a function instead:

function Set-Clipboard-Func {     $str = $input | Out-String      [Windows.Forms.Clipboard]::Clear();      if ( ($str -ne $null) -and ($str -ne '') ) {         [Windows.Forms.Clipboard]::SetText( $str )     } } 
like image 39
Tahir Hassan Avatar answered Sep 19 '22 03:09

Tahir Hassan