4

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.

1 Answer 1

4

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[]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.