powershellpowershell-2.0

PowerShell: Capture the output from external process that writes to stderr in a variable


I need to capture the output of a external process into a variable (string), so I can do some processing on that.

The answer from here works nicely as long as the process writes to stdout. However, if the process fails, it writes to stderr. I'd like to capture this string too, and I can't figure out how to do it.

Example:

$cmdOutput = (svn info) | out-string

This works, unless SVN has an error. If an error occured, SVN writes to stderr, and $cmdOutput is empty.

How do I capture the text written to stderr in a variable in PowerShell ?


Solution

  • Try this:

    $cmdOutput = svn info 2>&1