What Is Try Catch in PowerShell & How to Use It

Here's how to handle errors with simple command-line cmdlets

by Claire Moraa
Claire Moraa
Claire Moraa
Author
Claire likes to think she's got a knack for solving problems and improving the quality of life for those around her. Driven by the forces of rationality, curiosity,... 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 you are a frequent PowerShell user, you have obviously encountered a screen full of red text indicating an error.
  • It can be difficult to pinpoint the exact cause of the error but with PowerShell’s try-catch command, you can catch the error and act on it.
  • In this article, we show you how to get started with the try catch command.
XINSTALL BY CLICKING THE DOWNLOAD FILE
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.

Try catch is a control flow statement that is used within PowerShell to catch errors and exceptions. It can be used when you want to handle errors in your script and stop it from proceeding further.

Although PowerShell is a powerful Windows command-line tool, sometimes, it can be hard to interpret if it returns an error. You might be stuck for days trying to figure out where you went wrong. That’s where the try-catch command comes in, and we show you how to use it in this article.

Is there a try catch in PowerShell? 

Try Catch statements are a powerful way of handling errors and exceptions in PowerShell. They can handle all kinds of errors, from system exceptions to user-generated errors.

Before you understand how they work, you first need to know what they mean and what they can do.

Try Block

A try block is the first block of code that executes when an error occurs. It can run any code, but it doesn’t do anything special for errors. If an error occurs, PowerShell will continue executing the rest of the script without waiting for you to deal with the error. 

A Try Catch statement is comprised of one or more Try blocks. These blocks must be placed in a specific order for the code to execute properly.

This block begins executing immediately if no error has occurred. If an error occurs during this block, it will exit that block immediately and continue executing the code within the matching Catch block(s).

Catch Block

The catch block is a part of the PowerShell pipeline that can handle errors and other exceptions. The catch block does not return any value, but it does allow you to handle the error condition in your script.

This block contains the code that handles errors while executing code within the Try block(s). It will execute if an error occurs during any Try block within the script.

The try-catch construct allows you to handle different errors in your script. For instance, if you have a function that does not return a value, you can use the try-catch construct to check for an error condition and then perform some action if there was one.

How do you write try catch in PowerShell?

Expert tip:

SPONSORED

Some PC issues are hard to tackle, especially when it comes to corrupted repositories or missing Windows files. If you are having troubles fixing an error, your system may be partially broken.
We recommend installing Restoro, a tool that will scan your machine and identify what the fault is.
Click here to download and start repairing.

It is important to note that during this scripting test, we shall be using the Windows PowerShell ISE (Integrated Scripting Environment). This is the Windows PowerShell interactive shell experience. 

ISE provides an excellent development environment for writing, debugging, and testing scripts with an interactive command prompt. It includes a full suite of integrated scripting, debugging, and editing tools that enable you to rapidly develop, test and debug PowerShell scripts.

The ISE also includes script debugging features that allow you to step through your code interactively.

  1. Hit the Windows key, type Powershell ISE and click Run as administrator.
  2. Come up with a code that will give a positive error. The syntax for a try-catch block is: 

Try { 
// Code that might cause an error 
} catch { // Code to execute if an error occurs during execution of the try block
}

  1. As an example, we can use the following: 

Try {
Write-Host "This will not throw any exception."
} Catch [System.Exception] {
Write-Host "This will throw an exception." }

While the two blocks can run and catch errors, you need another block to clean up the error. The Finally block is used to clean up the environment after the execution of the try block.

How do I try catch if there’s no error in PowerShell?

An error is a condition that occurs while the PowerShell script is running and prevents the script from continuing to the next line of code. It can be caused by something external (like a missing file or invalid variable) or internal (a syntax error).

PowerShell has two types of errors: terminating and nonterminating. A terminating error stops the execution of the script. On the other hand, a nonterminating error is one that doesn’t stop the execution of the script but does generate output to the console.

The try/catch block allows us to handle errors by catching them with the try keyword and then providing an action block that will be executed if an error occurs. When an error occurs, PowerShell executes the code in the Catch block.

If no error occurs, the catch block will not execute, and the script will continue as normal. If the try catch in Powershell is not working, check out our detailed article on how to fix it.

Let us know how you use the try catch command line in the comment section below.

Still having issues? Fix them with this tool:

SPONSORED

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 TrustPilot.com) to easily address them. After installation, simply click the Start Scan button and then press on Repair All.

This article covers:Topics: