Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 526
Jamie Marshall Avatar asked Jul 13 '17 04:07

Jamie Marshall


1 Answers

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.

like image 115
vonPryz Avatar answered Oct 03 '22 16:10

vonPryz