Managing non-terminating errors

PowerShell Team

PowerShell Team

Most errors which occur in your working scripts are likely to be “non-terminating”.  This means that Monad just reports the error and the command keeps running.  (“Terminating” errors such as syntax errors will halt the command and, in some cases, the entire script; see for more details.). 

This Blog describes how to handle non-terminating errors.  You may choose to modify the action that occurs when a non-terminating error occurs or you may choose to collect non-terminating errors in a variable.  If you collect non-terminating errors in a variable, your script can check to see if any the prior command emitted any non-terminating errors.


MSH C:\temp\monad> remove-item nosuchfile.txt;write-host “done”
remove-item : Cannot find path ‘C:\temp\monad\nosuchfile.txt’ because it does not exist.
At line:1 char:12
+ remove-item  <<<< nosuchfile.txt;write-host “done”
MSH C:\temp\monad>

Even though the file was not found, the script just kept on running.  This is often the case with scripts which are syntactically correct but encounter operational errors.  So, what can you do to cause a script to react to problems it might encounter?

(1) -ErrorAction 

You can change how Monad reacts to non-terminating errors with the “-ErrorAction” ubiquitous parameter (“-ea” is the alias).  Your options are SilentlyContinue, Continue, Stop, and Inquire (also see

  • “SilentlyContinue”: Do not print, continue
  • “Continue”: Print, continue (this is the default)
  • “Stop”: Halt the command or script
  • “Inquire”: Ask the user what to do

You can suppress printing errors for a specific cmdlet with “-ea SilentlyContinue”, or in general with “$ErrorActionPreference = “SilentlyContinue””.  You can cause non-terminating errors to become terminating (the type will be ActionPreferenceStopException) with “-ea Stop” or “$ErrorActionPreference = “Stop””.

MSH C:\temp\monad> remove-item nosuchfile.txt -ea Stop
remove-item : Command execution stopped because the shell variable “ErrorActionPreference” is set to Stop: Cannot find path ‘C:\temp\monad\nosuchfile.txt’ because it does not exist.
At line:1 char:12
+ remove-item  <<<< nosuchfile.txt -ea Stop
MSH C:\temp\monad> remove-item nosuchfile.txt -ea SilentlyContinue
MSH C:\temp\monad>

(2)  -ErrorVariable

You can save off the non-terminating errors to a shell variable using the “-ErrorVariable” ubiquitous parameter (“-ev” is the parameter alias).  This is not affected by “-ErrorAction”.

MSH C:\temp\monad> get-content errorvariable.msh
remove-item foo.txt -ErrorVariable errs -ErrorAction SilentlyContinue
if ($errs.Count -eq 0)
    write-host “ran fine”
    write-host “failed”
MSH C:\temp\monad> .\errorvariable.msh
MSH C:\temp\monad>

You can also append failures to an existing ErrorVariable by prepending “+” to the variable name.  This even works for multiple cmdlets in the same pipeline appending to the same ErrorVariable.

MSH C:\temp\monad> gc errorvariable.msh
$ErrorActionPreference = “SilentlyContinue”

remove-item nosuchfile.txt -ErrorVariable errs
remove-item nosuchfile2.txt -ErrorVariable +errs
MSH C:\temp\monad> .\errorvariable.msh


[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]


Comments are closed. Login to edit/delete your existing comments