Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, December 13, 2017 6:22 AM | 1 vote
I have to run a powershell command from a batch file. stdout of the command is redirected to a file. I have noticed when the command fails that the error output is sent to stdout. For example consider this:
powershell -c "doesnotexist" 1>foo
I would expect that when I execute that command that the error would be shown because stderr is not redirected, however the error is actually sent to stdout. Is there a way I can make it so the command I execute via -c has its stderr sent to stderr of powershell.exe?
All replies (1)
Saturday, December 23, 2017 2:47 AM âś…Answered
Thanks but I'm trying to do it as a one-liner from the command prompt. I found this answer on stackoverflow that says if stderr is redirected to a file then powershell will send the output to stderr, so for now I'm going to do that. In other words
REM Error output is sent to stdout (foo):
powershell -c "doesnotexist" 1>foo
REM Error output is sent to stderr (bar):
powershell -c "doesnotexist" 1>foo 2>bar
It's confusing but I noted it in a comment for anyone else that has to work on the script. Otherwise I think you're right, I'd have to do it in a powershell script. It may also be possible to capture the error in a one-liner and then send it to stderr via [Console]::Error.WriteLine() but that seems less straightforward than what I have above.