When a PowerShell command has an error, that error may be one of two types.
A terminating error is one where the command cannot possibly continue and the command will always stop
terminating errors are less common.
A non terminating error is one where the command must stop its current action.
However, it can continue to process other actions.
For example, consider the following command Get my object class when 32 underscore service computer
name bad elo DC one.
If you assume that the computer bad does not exist on the network, get my object will have an error
when it tries to query that computer.
However, the command could potentially continue with the next computer.
L1 DC one.
The error is therefore a non terminating error dollar error action preference.
Windows PowerShell has a built in global variable named error action preference.
When a command has a non terminating error, the command checks that variable to see what it should
do.
The variable can have one of four possible values continue as the default and tells the command to display
an error message and to continue to run silently.
Continue tells the command to display no error message but to continue running.
Enquire tells the command to display a prompt asking the user what to do.
Stop tells the command to treat the error as terminating and to stop running, for example.
Dollar error action preference equals enquire.
If you intend to trap an error within your script so that you can handle the error, commands must use
the stop action.
You can trap and handle only terminating errors.
However, it is considered a poor practice to change dollar error action preference if the error that
you expect will be produced by a Windows PowerShell command.
Error action parameter.
All Windows PowerShell commands have an error action parameter.
This parameter has the alias.
The parameter accepts the same values as dollar error action preference, and the parameter overrides
the variable for that particular command.
If you expect an error occurring on a command, you therefore use error action to set that commands.
Error action to stop.
Doing this lets you trap and handle the error for that command, but leaves all other commands to use
the action in error.
Action Preference.
For example, get my object class win32 underscore service computer name bad l1 DC one error action
stop.
The only time that you will usually modify your reaction preference is when you expect an error outside
of a Windows PowerShell command, such as when you are executing a method.
Get process.
Name notepad for each object dollar piece item kill.
In this example, the kill method might have an error, but because it is not a Windows PowerShell command,
it does not have an error action parameter.
You would instead set dollar error action preference to start before running the method and then set
the variable back to continue after you run the method.
Note You may find examples elsewhere that set dollar error action preference to silently continue at
the beginning of a script.
Be aware that this will suppress all error messages in the whole script.
This would make debugging and troubleshooting very difficult.
You should usually remove that setting before you run the script in your environment so that any errors
will be displayed.