What's PowerShell equivalent of exec redirection in bash like this?
exec 2>&1
I wanted to make a PowerShell script to redirect its stderr to stdout by itself, without any function or script block calls like this, or recursive script invocation.
The redirection should occur in the beginning of the script, then all stderr output from the rest of the script and sub-process should go to stdout.
You can't: Unlike Bash, PowerShell has no feature for script-wide redirection of streams (as of PowerShell v7).
You'll have to do one of the very things you're trying to avoid:
Enclose all code in your script in . { ... } 2>&1
Recursively invoke your script via such an enclosure (which requires some mechanism, such as a conceptually private parameter, to prevent infinite recursion).
Also note that while stdout and stderr are the conceptual equivalents of PowerShell's success output stream and error stream, respectively:
PowerShell has 6 output streams in total - see about_Redirection (which your question already links to).
Regrettably, all of these streams are by default mapped to stdout when PowerShell is called from the outside, via its CLI.
2> from the outside - but none of the other streams, aside from the success stream.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With