newspaint

Documenting Problems That Were Difficult To Find The Answer To

How to Redirect Stderr From Executable in PowerShell 2.0 Without Line Breaks at Console Width

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: