Understanding The PowerShell Error Variable

Handling errors can be very tricky. With PowerShell, error variables can assist us in our troubleshooting endeavors.Fortunately, Jason knows the tricks of the trade and knows where the hidden $Error automatic variable is in PowerShell.
In this snip we’re going to focus on the automatic global error variable. Now the error variable is an array list and if you run the gettype method, we will see that. Like all arrays in powershell, the most recent object moves to the top of the array. If we look at the count property on the error variable we can see that we have zero errors. Now let's generate a couple of errors. As you can see from the red below we have generated 2 different errors. Looking at the count property again we can see that both errors have been stored in the error variable. We can run the clear method and remove the error records from the error variable. Looking at the count property again we can see that both errors have been removed. Is important to note that you need to run the clear method with caution because it could potentially clear errors you need to investigate later to aid in troubleshooting your code. Now let's generate another error since we cleared our previous ones. Looking at index zero we see that it stores the most recent error record that we just generated. Now let's generate one more error with the get alias cmdlet. And looking at index zero again. We see that our most recent error was added to the top. Looking at index one we see our error record from the get process cmdlet. Running the gettype method on index zero. We see the object type is an error record. We can put the error to the get member cmdlet and read all of the methods and properties available to us. Looking at the category info property, we can determine the cmdlet name. The error exception that was thrown. The reason for the error. The name of the object. We passed as well as the type of object that was passed to the Cmdlet. In our example, we passed the string Mark Twain to the get alias cmdlet and the item not found exception was thrown. Now let's look at the invocation info property. It provides quite a bit of useful information to help us troubleshoot exactly where the error was thrown. We can see the cmdlet that threw the error. The line and line number that generated the error. And if this error had been generated from a script instead of the console it would also provide the script name. The invocation property is very helpful when trying to troubleshoot a specific error in a script that is scheduled or running in the background. And if you look at the gettype method on our exception. We can determine the type of exception that was thrown. In this case, it was an item not found exception. Check out my other snip on using the try and catch script blocks to see how these exception types can be used to improve error handling. An additional method that is available to us is the clone method here. We are cloning all of our error records into the error record variable. Looking at the count property, both error records have been cloned to our variable. Checking out index zero we see our get alias error from before. And if we pipe index zero to get member. We will see that it’s an error record object type. The con method is useful when you need to clear the error global variable but need to further troubleshoot. The existing error records and did not want to lose them. This covers understanding the power shell error variable. Thank you for watching.