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
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With