I have a PowerShell script on my desktop:
Write-Output "Some output"
Write-Error "Non-fatal error"
$ErrorActionPreference = "Stop" # Any further errors should be treated as fatal
Write-Output "Some more output"
Write-Error "Fatal error"
I call it this way from powershell.exe:
PS C:\Users\me\Desktop> .\test.ps1 2> err.txt 1> out.txt
Output:
C:\Users\vmadmin\Desktop\test.ps1 : Fatal error
At line:1 char:1
+ .\test.ps1 2> err.txt 1> out.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
out.txt:
Some output
Some more output
err.txt:
C:\Users\vmadmin\Desktop\test.ps1 : Non-fatal error
At line:1 char:1
+ .\test.ps1 2> err.txt 1> out.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
I would like the fatal error to get logged to err.txt. How can I accomplish that?