Share this article

Improve this guide

How to Fix Try Catch PowerShell Not Working?

To fix this look for incorrect syntax

4 min. read

Updated onOctober 4, 2023

updated onOctober 4, 2023

Share this article

Improve this guide

Read our disclosure page to find out how can you help Windows Report sustain the editorial teamRead more

Key notes

The Try-Catch statement inPowerShellhandles 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?

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:

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

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

1. Check Syntax

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:

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:

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:

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

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.

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

Srishti Sisodia

Windows Software Expert

Srishti Sisodia is an electronics engineer and writer with a passion for technology. She has extensive experience exploring the latest technological advancements and sharing her insights through informative blogs.

Her diverse interests bring a unique perspective to her work, and she approaches everything with commitment, enthusiasm, and a willingness to learn. That’s why she’s part of Windows Report’s Reviewers team, always willing to share the real-life experience with any software or hardware product. She’s also specialized in Azure, cloud computing, and AI.

User forum

0 messages

Sort by:LatestOldestMost Votes

Comment*

Name*

Email*

Commenting as.Not you?

Save information for future comments

Comment

Δ

Srishti Sisodia

Windows Software Expert

She is an electronics engineer and writer with a passion for technology. Srishti is specialized in Azure, cloud computing, and AI.