Maybe there's no way to do this in PowerShell.
If I have the file verboseTest.ps1
[CmdletBinding()] # CmdletBinding attribute enables -verbose flag
Param()
Write-Verbose 'verbose test'
If I call .\verboseTest.ps1
, I see nothing as expected.
If I call .\verboseTest.ps1 -Verbose
I see the output.
VERBOSE: verbose test outer
as expected.
But if I call .\verboseTest.ps1 4>&1
I don't. The verbose stream is lost.
Now, Info behaves completely differently.
I have the file infoTest.ps1
and it behaves sanely.
[CmdletBinding()] # Add CmdletBinding attribute
Param()
Write-Information 'info test outer'
If I call .\infoTest.ps1
, I see nothing as expected.
If I call .\infoTest.ps1 -InformationAction 'Continue'
, which is analogous to the -Verbose
flag, it succeeds and outputs info test outer
to the console.
And if I call .\infoTest.ps1 6>&1
it also outputs info test outer
to the console! So somehow the "information" stream behaves completely differently from the "verbose" stream.
The behavior of the info stream makes sense. The write-information commands write to the stream, and I can redirect it or view it or not as I see fit. The "verbose" does not! I have to enable showing it in the output in order to redirect it? Am I doing something wrong? How does this make sense?
Do I have to enable verbose preference in order to redirect the output? And if so, can I do that inside the method or does it change it globally session-wide?