Although error action tells a command what to do when an error happens, it does not capture the error.
There are two ways to capture an error in Windows PowerShell.
The first is to use the built in error variable.
This variable is an array and the first item in the array is always the most recent error.
The variable is global.
This means that it can contain errors from the whole shell session.
Accessing error zero provides the most recent error.
This array will contain all errors, even those that resulted from an action other than running a Windows
PowerShell command.
For example, if you execute a method or an external application and an error happens, dollar error
will contain that error.
When you run a Windows PowerShell command.
A better approach is to use the error variable parameter which uses the alias ivy.
The parameter accepts a variable name as its value, and if an error happens, the error is stored in
that variable.
Note Variable names do not include a dollar sign dollar.
Make sure not to include a dollar sign when specifying a variable name to error variable.
For example.
Try to get WMI object class win32 underscore service computer name dollars name error action stop error
variable minor.
Catch.
Right.
Verbose error connecting to dollar name dollar maker out file c colon backslash errors dot text.
In this example, the error is captured to my error and written to the file.
See colon backslash errors.
Dot text.