PowerShell has a problem with it's extra output streams. The actual content of the Warning, Verbose, Debug, Information, and even Error streams doesn't have the label text like "WARNING: " or "VERBOSE: " that we're used to seeing in the host. That label is actually added by the host (hopefully, in a culture-aware way). However, this means that when you attempt to redirect all of this output, for example by redirecting all output streams to stdout, with *>&1
, you don't get labels on them at all, which is confusing, and can make the output difficult to comprehend.
Take for example a function that writes in a loop:
if ($i % 5 -eq 0) {
Write-Output $i
} else {
Write-Verbose $i
}
If you redirect Verbose to StdOut, then outside of the host, you can no longer tell which line was the output line(s), and which were verbose. So if you, for instance, pipe to | Out-String
or to a file, the extra information is lost.
The functions in this WithOut module fix that in one of the ways it could be fixed -- not necessarily the best way, but one way.
So far I've wrapped Out-String
and Out-File
as those are the two I've used. The new functions have switches to turn on color (-VtColored
), to convert errors to the detailed view (-ExtendedError
) and to -AddTimestamps
(they're only added to non-output lines, that is, to Verbose, etc).
Note that in PowerShell, only the standard output stream goes down the pipeline, so these functions only capture all the output when you redirect it, like this:
Test-Output *>&1 | Out-String -VtColored | less
Or like this:
Test-Output -Verbose -Debug -Skip 2 -First 5 *>&1 | Out-File log.ansi -VtColored -ExtendedError -AddTimestamps