As Santiago Squarzon notes, 2>&1 is a redirection (>) that redirects output stream number 2 (the error stream) into (&) stream number 1 (the success stream).
- When calling external programs,
2 in redirections refers to such a program's stderr output, and 1 (the default stream) to its stdout output.
What your function in effect does is to promote any error - irrespective of whether it is a non-terminating or a (statement-)terminating error - to a fatal error (script-terminating aka runspace-terminating error).
However, I propose a simpler implementation, which also avoids the use of Invoke-Expression, which is generally to be avoided, by instead requiring that a script block ({ ... }) rather than a string be passed as an argument:
function Assert-NoError {
param(
[scriptblock] $cmd
)
$ErrorActionPreference = 'Stop'
. $cmd
}
You would then invoke the function as follows, for example:
Assert-NoError { Get-Process test }
Note:
$ErrorActionPreference = 'Stop' creates a local instance of the the $ErrorActionPreference preference variable, and by setting it to Stop ensures that any error results in a fatal error by default.
The above implementation has the added advantage that any success output that precedes an error is output before execution is aborted.