The verbose, warning, and debug streams are merged into STDOUT when you run PowerShell scripts via
powershell -File "C:\myscript.ps1"
so you can't redirect them separately anymore. Only the error stream is different, since it seems to go to both STDOUT and STDERR, where it can be redirected by 1>
as well as 2>
.
Demonstration:
C:\>type test.ps1
$DebugPreference = "Continue"
$VerbosePreference = "Continue"
Write-Output "Output message"
Write-Error "Error message"
Write-Verbose "Verbose message"
Write-Warning "Warning message"
Write-Debug "Debug message"
C:\>powershell -File .\test.ps1
Output message
C:\test.ps1 : Error message
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
VERBOSE: Verbose message
WARNING: Warning message
DEBUG: Debug message
C:\>powershell -File .\test.ps1 2>nul 3>nul 4>nul 5>nul
Output message
VERBOSE: Verbose message
WARNING: Warning message
DEBUG: Debug message
C:\>powershell -File .\test.ps1 1>nul
C:\>_
If you want to redirect the verbose, warning, or debug stream separately you must use -Command
instead of -File
and do the redirection within PowerShell:
C:\>powershell -Command ".\test.ps1 2>$null 3>$null 5>$null"
Output message
VERBOSE: Verbose message
However, while in CMD you can redirect any handle to any other handle (3>&2
, 1>&5
, ...), PowerShell redirection only supports redirection to either a file (3>C:\out.txt
) or the success output stream (3>&1
). Trying to redirect to any other stream will throw an error:
C:\>powershell -Command ".\test.ps1 2>out.txt 3>&2"
At line:1 char:22
+ .\test.ps1 2>out.txt 3>&2
+ ~~~~
The '3>&2' operator is reserved for future use.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RedirectionNotSupported
as will redirecting different streams to the same file:
C:\>powershell -Command ".\test.ps1 2>out.txt 3>>out.txt"
out-file : The process cannot access the file 'C:\out.txt' because it is being
used by another process.
At line:1 char:1
+ .\test.ps1 2>out.txt 3>>out.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], IOException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
If merging warning and success output is an option for you, you could do something like this:
powershell -Command ".\test.ps1 >out.txt 3>&1 2>error.log"
or like this:
powershell -Command ".\test.ps1 >out.txt 3>&1 2>&1"
or (redirecting all streams) like this:
powershell -Command ".\test.ps1 *>out.txt"
Otherwise the only option I see is to redirect to different files:
powershell -Command ".\test.ps1 3>warning.log 2>error.log"