How to Redirect Stderr From Executable in PowerShell 2.0 Without Line Breaks at Console Width
March 21, 2017
Posted by on
So you think PowerShell is flexible and useful and you go to redirect output from an external command to a file. You’ll parse this file later. But when you do you are horrified to find line breaks inserted in the middle of lines in the file… at the width of the console in which your PowerShell session was running.
The problem is that PowerShell is so smart that it unhelpfully inserts line breaks in all output at the current console width.
There are a few suggested answers to this including adding a -Width parameter to Out-File, but this doesn’t solve the issue for stderr redirects.
The solution was found in this post. Simply put, run your external command through cmd.exe and redirect from there!
To ensure the redirect operator is passed to cmd.exe escape the angle bracket with a backtick.
cmd.exe /C wget.exe -O - http://www.google.com/ 2`>stderr.log