Windows PowerShell uses the tri catch finally construct to implement error handling.
Note Windows PowerShell also uses the trap construct for error handling.
This construct was introduced in Windows PowerShell 1.0 and is more difficult and less flexible than
try catch.
Finally, you should use try catch finally in new scripts.
As the name implies, the construct consists of up to three sections.
The try section contains commands that may have an error.
These commands must have their error actions set to stop, or the error action preference variable must
be set to stop.
This section is required.
The catch section is optional and will run if an error happens in the try section.
The final section is optional and will run regardless of whether an error happens in the try section
if it is used.
Finally must come after catch.
You do not have to use both catch.
And finally, however, you must use at least one of them.
You can have multiple catch sections configured so that each section handles a different kind of error.
For more information about how to do this, read the above underscore try underscore.
Catch underscore.
Finally help file in Windows PowerShell.
Here is an example.
Try get WMI object class win32 underscore service computer name dollars name error action.
Stop.
Catch.
Right.
Verbose error connecting to dollar name.
Note The final section of this construct is rarely used.
That is why the construct is known as tri catch instead of tri catch.
Finally, remember that setting error action to stop will turn any error into a terminating error.
That means the command will not continue to process.
For this reason, you will usually write your script so that the command is processing only one thing
at a time.
In this example, that means the name would contain only one computer name at a time.
So if one computer name fails, the command is not skipping anything.