HOWTO: Find Specific Exceptions to Use with Try/Catch in PowerShell

Consider the following scenario. You run a command in PowerShell. It fails. It generates tons of scary red text. You don’t like this. You want to display a more friendly error message. Or better still, you want to take some action based on the error.

With PowerShell, you can do this easily with something called try / catch statements. There is lots of documentation online for how to use these. However, one thing I found difficult to figure out was how to define specific exceptions. Consider this code block:

Try {
# Do something
Catch [SpecificExceptionGoesHere]
{ # Do something else if the above fails }

The problem I’ve had is figuring out what exactly you type in the yellow box. I’m pleased to report I now have an easy way of figuring it out.
To do so, you first need to intentionally cause the error you want to capture. Once you’ve done that you can extract the exception text.

Oh, I should probably mention that PowerShell automatically saves all Errors in a custom variable called $Error and that the most recent error is always available from $Error[0]

Let’s dive straight into an example. Below we’ve run a simple cmdlet (in this case Get-ChildItem) against a file we know doesn’t exist. It generates an error.

In this case, the error is that the file does not exist. This command could generate other error messages as well such as “Access Denied” but we are only interested in the “not existing” scenario. To figure out how to capture that type:


It’ll return the name you need to put in the yellow text above. Now that you have that, look what we can do:

try { Get-ChildItem c:\thisfiledoesnotexist -ErrorAction Stop }

catch [System.Management.Automation.ItemNotFoundException]

{ Write-host “This block is executed instead of the error written to the screen” }

(Note: Most of the time the –ErrorAction Stop will be required for try/catch to work. If you want to know more, look up “terminating versus non-terminating errors” in Google/Bing/Etc)

I’m going to be using this a lot, that’s for sure. The possibilities are endless!



Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.