Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell live output from an external command

Tags:

powershell

In our powerscript, I could do something as follows:

$output = myexternalcommand
write-output $output

This works. However, the output is displayed only after the external program finishes running. Although the external program is dumping status periodically, there is no indication of what is going on from within the script.

I am wondering if there is a way to display the output of the external program as it is running. Regards.

like image 514
Peter Avatar asked Oct 17 '25 18:10

Peter


1 Answers

Use the Tee-Object to send external command's output to two directions. As per the documentation,

The Tee-Object cmdlet redirects output, that is, it sends the output of a command in two directions (like the letter "T"). It stores the output in a file or variable and also sends it down the pipeline. If Tee-Object is the last command in the pipeline, the command output is displayed at the prompt.

cmd /c "dir /s c:\windows\system32" | Tee-Object -Variable foobar
# dir list is printed and result is also stored in $foobar
$foobar.Count # will return the dir cmd's output as an Object[]
like image 107
vonPryz Avatar answered Oct 19 '25 08:10

vonPryz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!