-ErrorAction argument ignored in favor of $ErrorActionPreference

Question:

Both, $ErrorActionPreference variable and -ErrorAction cmdlet argument should define behavior for non-terminating errors.

Since variable is global and argument is specific to cmdlet invocation I expect that argument overrides variable. In fact, I can’t find a situation, where -ErrorAction does anything at all.

P.S. – I’ve found this question but it is more focused on related issue rather on priority.

Answer:

The -ErrorAction parameter applies to errors generated by the command it is applied to. In the case of Invoke-Expression (iex) that means errors encountered while preparing to invoke the passed expression or errors encountered post processing the results. It doesn’t, however, apply to the expression/command itself.

In the OP if you apply -ErrorAction to the Write-Error command you will get the expected results, whether or not you use iex.

Source:

-ErrorAction argument ignored in favor of $ErrorActionPreference by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply