Search code examples
powershellstdoutstderr

Powershell redirect stderr and stdout to two different places


How do redirect

  • stderr to logfile
  • stdout to object

Things I've looked at:

>> and 2>> only redirect to file .
-RedirectStandardOutput and -RedirectStandardError only redirect to file again.
| Out-File cannot redirect stderr.
| Tee-Object same issue.


Solution

  • Joining stdout and stderr output streams works like PetSerAl commented, though the syntax is not the most intuitive.

    The weirdish syntax of 2>&1 means that stderr (stream 2) is to be added into the stdout (stream 1). As this is not actually what you are after, try adapting the other example from the MS page to Powershell:

    Or, you can redirect the output to one place, and the errors to another.

    dir file.xxx > output.msg 2> output.err

    Thus,

    $ret = myCommand 2> errors.log
    

    should send errors in a log file and non-errors in the $ret variable.