Multi-page, warning, and debugging streams merge into STDOUT when running PowerShell scripts through
powershell -File "C:\myscript.ps1"
so that you cannot redirect them separately. Only the error stream is different, because it seems to be suitable for both STDOUT and STDERR, where it can be redirected to 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 flow of details, warnings, or debugging separately, you must use -Command instead of -Command and do the redirection in 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 descriptor to any other descriptor ( 3>&2 , 1>&5 , ...), PowerShell redirection only supports redirecting a file ( 3>C:\out.txt ) or the success output stream ( 3>&1 ). Attempting to redirect to any other thread 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
since it redirects 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 you want to merge warning and success, you can 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 (redirect all threads) as follows:
powershell -Command ".\test.ps1 *>out.txt"
Otherwise, the only option I see is redirection to different files:
powershell -Command ".\test.ps1 3>warning.log 2>error.log"