Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clip and display result in same time

Tags:

powershell

How can I use clip and display output at the same time?

Every time if I put | clip at the of the end line, the output is copied to clipboard, but not displayed in the console window.

Get-Date | clip
like image 430
LamiX Avatar asked Sep 06 '17 21:09

LamiX


2 Answers

Use common parameter -ov (-OutVariable) to also capture Get-Date's output in a variable, then output that variable:

Get-Date -ov output | clip; $output

If the command you're invoking is not a cmdlet or advanced function/script and therefore doesn't support -OutVariable, you can use this technique instead:

($output = Get-Date) | clip; $output

This relies on the fact that enclosing a variable assignment in (...) passes the assigned value through.


You can package this functionality with the help of a custom function:

Function Write-OutputAndClip { $Input | Write-Output -ov output | clip; $output }

If you also define an alias for it, say clipecho ...

Set-Alias clipecho Write-OutputAndClip

... you can invoke it succinctly as:

Get-Date | clipecho  # copies output to the clipboard *and* echoes it.
like image 190
mklement0 Avatar answered Oct 22 '22 11:10

mklement0


After looking into this a bit I think the only way to do what you're asking is to do it in two separate lines.

$value = Get-Date
Write-host $value
$value | clip

One line would look like this $value = Get-Date;$value;$value|clip

Powershell really wants to redirect anything from write-host to the console. And clip doesn't want to pass anything further down the pipeline...

like image 1
Jason Snell Avatar answered Oct 22 '22 10:10

Jason Snell