How to Fix Try Catch PowerShell Not Working?

To fix this look for incorrect syntax

by Srishti Sisodia
Srishti Sisodia
Srishti Sisodia
Srishti is an avid writer with a keen interest in technology. She has spent the past ten years of her career researching and writing troubleshooting articles for Windows... read more
Reviewed by Alex Serban
Alex Serban
Alex Serban
Windows Server & Networking Expert
After moving away from the corporate work-style, Alex has found rewards in a lifestyle of constant analysis, team coordination and pestering his colleagues. Holding an MCSA Windows Server... read more
Affiliate Disclosure
  • 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

To fix various PC problems, we recommend Restoro PC Repair Tool:
This software will repair common computer errors, protect you from file loss, malware, hardware failure and optimize your PC for maximum performance. Fix PC issues and remove viruses now in 3 easy steps:

  1. Download Restoro PC Repair Tool that comes with Patented Technologies (patent available here).
  2. Click Start Scan to find Windows issues that could be causing PC problems.
  3. Click Repair All to fix issues affecting your computer's security and performance
  • Restoro has been downloaded by 0 readers this month.

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

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.

Still having issues? Fix them with this tool:


If the advices above haven't solved your issue, your PC may experience deeper Windows problems. We recommend downloading this PC Repair tool (rated Great on to easily address them. After installation, simply click the Start Scan button and then press on Repair All.

This article covers:Topics: