Echoing The Error Channel

by Jul 6, 2011

To control which output from a batch file is considered "a result" and which output should rather always be visible to the user, you can redirect information to StdErr when you want it to be excluded from the results. Take a look at this batch file:

@echo off
echo Starting...
netstat
echo Done!

 

When you run it from PowerShell and save its results, all information is stored in $result, including your messages to the user:

$result = .\test.bat

 

This version makes sure the user messages remain visible and do not get redirected to the variable:

@echo off
echo Starting... 1>&2
netstat
echo Done! 1>&2

 

Twitter This Tip!
ReTweet this Tip!