Capture process output in PowerShell.

I am using the following code to run an external application from powershell:

$Process = New-Object System.Diagnostics.Process
$Process.StartInfo.FileName = "..."
$Process.StartInfo.WorkingDirectory = "..."
$Process.StartInfo.RedirectStandardError = $true
$Process.StartInfo.RedirectStandardOutput = $true
$Process.StartInfo.UseShellExecute = $false
$Process.StartInfo.Arguments = "..."




Open in new window

What I need is a way to dynamically capture any output that the application makes i.e. not wait for the application to exit before returning the output. The application is long running and reports status information back to the user which I would like to output as part of the powershell script. I have been unable to find anything online that does this but I am sure this must be possible?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You're overcomplicating things.
Powershell is first and foremost a shell that has the ability of starting programs as one of its main features, not a programming language, so there's usually no need to start external programs using .net objects.
Just start the program; the output will be send down the pipeline automatically as soon as it's produced (and end up in the console if the command it's the last element of the pipeline).
Example (no, it will not really copy anything, just create lots of output and take a while):
& "C:\Windows\system32\robocopy.exe" "C:\Windows" "C:\DUMMY_NEVER_USED" *.* /e /L

Open in new window

Or if you just want it on the console host instead of the pipeline:
& "C:\Windows\system32\robocopy.exe" "C:\Windows" "C:\DUMMY_NEVER_USED" *.* /e /L | Write-Host -ForegroundColor Yellow

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Blowfelt82Author Commented:
Worked like a charm - thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.