Reputation: 47
I need to redirect the STDERR and STDOUT to two separate files.
I tried the following:
This will output error only to an error file:
powershell.exe -file c:\test.ps1 2> test.txt
This will output all output to a result file:
powershell.exe -file c:\test.ps1 2>&1> test.txt
My question is how can I redirect STDERR and STDOUT to two separate files by running powershell.exe
just once?
Upvotes: 2
Views: 4979
Reputation: 54971
You can specify multiple redirections. Ex.
powershell.exe -file Sample.ps1 2>errors.txt 1>output.txt
Sample.ps1
Write-Error "This is a critial error"
Write-Output "This is output"
"This is also output"
Errors.txt
C:\Users\frode\Desktop\Sample.ps1 : This is a critial error
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Untitled202.ps1
Output.txt
This is output
This is also output
Upvotes: 3