How can I redirect stdout and stderr without polluting PowerShell error output

Your symptom implies that $ErrorActionPreference="Stop" is in effect at the time function
Test-Validation executes.
(Temporarily) set it to 'Continue' to fix your problem – which in future versions will hopefully no longer required (see below).

The reason for the observed behavior is that, in Windows PowerShell and up to PowerShell (Coe) 7.1, using an error-stream redirection (2>) makes PowerShell route an external program‘s stderr output through PowerShell’s error stream (see about_Redirection), and $ErrorActionPreference="Stop" therefore throws a script-terminating error once the first stderr line is received.

This behavior is unfortunate, because stderr output from external programs cannot be assumed to represent an error condition, given that external programs in effect use stderr, the standard error stream, for anything that other than data, which includes status information, for instance.

PowerShell 7.2 and above change this behavior for the better: stderr output is no longer routed through PowerShell’s error stream, which means that:

Leave a Comment