How to Fix Try Catch PowerShell Not Working?

To fix this look for incorrect syntax

Reading time icon 4 min. read


Readers help support Windows Report. When you make a purchase using links on our site, we may earn an affiliate commission. Tooltip Icon

Read the affiliate disclosure page to find out how can you help Windows Report effortlessly and without spending any money. Read more

Key notes

  • If Try-Catch is not working on PowerShell, you must check for incorrect syntax and error action values.
  • This guide will explain the different ways to resolve the issue.
Try catch powershell not working

The Try-Catch statement in PowerShell handles errors and exceptions in your code. If Try-Catch is not working as excepted on PowerShell, it can be frustrating. 

In this guide, we will cover all the ways to fix the issue along with the probable causes of the problem. Let’s start!

What causes the Try-Catch not working issue in PowerShell?

There could be various reasons why the statement is not working on PowerShell. Here are some of the most common ones: 

  • Default ErrorAction set – If the ErrorAction is set to Continue, then Try-Catch will not work as expected. 
  • Syntax errors – If the syntax used is improper, the formatting is incorrect, or it is missing keywords, Try-Catch will not work.
  • Cmdlet or function behavior – Some functions and cmdlets come with their error-handling behavior, which might not be compatible with Try-Catch.
  • Incorrect logic or use of variables – If there is an inappropriate use of variables or incorrect logic, then Try-Catch will not work. 

What can I do if the Try-Catch is not working on PowerShell?

1. Check Syntax

  1. First, you need to check if you have used the correct syntax for the version of PowerShell you are using.
  2. Next, verify that you have placed the Try and Catch blocks in the correct sequence.
  3. The basic syntax for Try-Catch is mentioned below. Replace # code to run here with the code and # error handling code here with the error handling code:

Try {
# code to run here
}
Catch {
# error handling code here
}

2. Check if you are trapping a specific error 

You need to verify if you are trapping specific errors you want to handle and not all the errors. Follow these steps to do the same:

  1. Press the Windows key, type PowerShell, and click Run as administrator.Script
  2. To trap an error, you need to use the $ variable in the Catch block to access information about the error that was thrown. Let’s check the example of the same to understand better: Try {
    Get-ChildItem -Path C:\DoesNotExist
    }
    Catch {
    if ($_.Exception.GetType().Name -eq "DirectoryNotFoundException") {
    Write-Output "Directory not found."
    }
    else {
    throw $_.Exception|
    }
    }

3. Set ErrorAction value 

The default value for ErrorAction is Continue; however, for Try-Catch to work, you need to set the ErrorAction value to Stop. To do this, follow these steps:

  1. Open PowerShell as administrator.
  2. You need to use the Set-StrictMode cmdlet or the ErrorAction parameter on individual cmdlets. To understand it better, let us look at an example: 
Try Catch with ErrorAction Stop PowerShell

Try {
Get-ChildItem -Path C:\DoesNotExist -ErrorAction Stop
}
Catch {
Write-Output $_.Exception.Message
}

4. Use the correct exception type

It is important to use correct exceptions when using Try-Catch, as it can only handle exceptions of the System.Exception type. If you have used non-System.Exception errors, then Try-Catch will not work.

As for non-System.Exception, you need to use the NET” framework’s error handling mechanism or catch the error.

5. Avoid script termination 

If your script is terminated before reaching the Catch block, Try-Cach will not get a chance to handle the error. To avoid script termination, follow these steps:

  1. Launch PowerShell with admin rights.
  2. You need to use the Continue statement after the Catch block. Let’s understand it by an example: 
try catch finally

Try {
Get-ChildItem -Path C:\DoesNotExist
}
Catch {
Write-Output $_.Exception.Message
Continue
}

6. Use the Try-Catch-Finally

 If the Try-Catch statement is still not working, you can use the Try-Catch-Finally construct instead. This ensures that your code runs to completion, even if an exception is raised.

  1. Open PowerShell as administrator.
  2. Type the command with Try-Catch-Finally. Here is an example to understand the script better: 

Try {
# Your code here

Catch [System.Exception] {
# Exception handling code here

Finally {
 # Clean-up code here
}

So, these are ways in which you can resolve the Try Catch not working issue on PowerShell. If you have any questions related to Try-Catch, please mention them in the comments section below. We will be happy to help!

If you want to know how to stop PowerShell from closing after running a script, you can read this.

More about the topics: PowerShell