Microsoft – PowerShell – Error Handling

Microsoft – PowerShell – Error Handling

Still struggling along with PowerShell and wishing someone will say “Walk a little Straighter” ( to me.

But, from reading through others lens, here is my current understanding.

At a higher level, for instance at a module level, one gets to choose how errors are handled.

To do so, one needs to set the system variable $ErrorActionPreference.

The various available options for $ErrorActionPreference:

a) SilentlyContinue – Errors are not displayed and Script Continues Execution without   visually acknowledging an error has occurred

b) Continue – Errors are displayed and Script continues execution

c) Stop – Errors are displayed and the Script is terminated

d) Inquire – Errors are displayed and user is prompted\asked how he \ she will like to proceed

To establish your choice, use something resembling:

$ErrorActionPreference = "SilentlyContinue"

Note that PowerShell also supports statement level “Error Reporting”.

My first initiation into this construct was from Steve Murawski’s Blog Entry.

Soccer is called the “Beautiful Game” and some words are best left hanging alone:

Deep Dive Error Handling: what happens when you encounter an error?

If you hit a terminating error, your script, function, or command will stop.  The error will be logged to the $error variable and written to the error stream.  The $? will be set to false.  You can use the trap construct (or the try/catch/finally construct in V2) to deal with terminating errors and we’ll cover that further in the fourth post in this series.

If you hit a non-terminating error, the result will depend on the $ErrorActionPreference in the scope or the –ErrorAction parameter.  If the $ErrorActionPreference or –ErrorAction parameter is set to ‘Continue’, the error will be written to the error stream, added to the $error array, and the $? will be set to $false.  ‘SilentlyContinue’ will not write an error to the Error stream, but $error and $? are updated.  Finally, ‘Inquire’ provides you an option at each error as to whether to treat it as terminating or non-terminating.  If treated as a non-terminating error, the error will be treated like the $ErrorActionPreference or –ErrorAction parameter is ‘Continue’.

From what I can surmise:

When an error is encountered, the following things happen:

  1. The System Array variable ($error) is updated; in this case a new entry is added to it
  2. A System Boolean variable ($?) is updated with the system keyword $false
  3. And, depending on the global variable $ErrorActionPreference or the statement level argument -ErrorAction, the error will be displayed or “eating up”
  4. At the statement level, errors can also be saved by using the “ErrorVariable” parameter


  1. Use PowerShell – Error Handling (Deep Dive)
  2. Hey, Scripting Guy! How Can I Use $ErrorActionPreference to Control Cmdlet Handling of Errors?
  3. Write Warning

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s