4

folks, I need to redirect command output to 2 files, in one file redirect stdout stream, to another file redirect stderr stream. Is it possible to do in cmd, PowerShell on windows ?

0

2 Answers 2

4

For powershell

Let us say you have my.ps1 like below:

"output"
Write-Error "error output"
exit 1

You can do:

.\my.ps1 2>stderr.txt | Tee-Object -file stdout.txt

You get stdout and stderr in the corresponding files.

More on Tee-Object:

http://technet.microsoft.com/en-us/library/dd347705.aspx

More on capturing all streams:

https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=297055&SiteID=99

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, for your answer, it's right, but it's not that i find. I am writing program on C# that execute hg(Mercurial) through "WScript.shell", how can i redirect output from there to 2 files, stdout and stderr ?
The original question was a bit unclear. Are you looking for this: Windows Scripting Host (WSH) output redirection?
1

In Powershell you can redirect standard output and error using the well known redirection operators >, >>, 2>, 2>>.

First make sure to set:

$erroractionpreference.value__=1

Then use redirection.

Examples:

ls C:\ 2> stderror.txt > stdoutput.txt # write output on stdoutput.txt

ls foo 2> stderror.txt > stdoutput.txt # write output on stderror.txt unless foo exists

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.