Skip to main content
St Louis

Back to all posts

How to Catch Exceptions on Powershell?

Published on
4 min read
How to Catch Exceptions on Powershell? image

Best PowerShell Script Tools to Buy in October 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

BUY & SAVE
$48.99
AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease
4 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$34.99
Learn Windows PowerShell in a Month of Lunches
5 PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

BUY & SAVE
$37.95
PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)
6 Learn PowerShell Toolmaking in a Month of Lunches

Learn PowerShell Toolmaking in a Month of Lunches

BUY & SAVE
$20.51 $44.99
Save 54%
Learn PowerShell Toolmaking in a Month of Lunches
7 Learn PowerShell Scripting in a Month of Lunches

Learn PowerShell Scripting in a Month of Lunches

BUY & SAVE
$45.28
Learn PowerShell Scripting in a Month of Lunches
8 PowerShell for Sysadmins: Workflow Automation Made Easy

PowerShell for Sysadmins: Workflow Automation Made Easy

BUY & SAVE
$28.99
PowerShell for Sysadmins: Workflow Automation Made Easy
+
ONE MORE?

In PowerShell, you can catch exceptions using the "try" and "catch" blocks. Within the "try" block, you place the code that may potentially throw an exception. Then, in the "catch" block, you can specify the type of exception that you want to catch, or simply catch all exceptions by not specifying a type. You can also use the "finally" block to execute cleanup code, which will run whether or not an exception is thrown. By using these blocks, you can handle exceptions gracefully and control the flow of your script effectively.

What is the error variable in PowerShell?

The error variable in PowerShell is called $error. It contains an array of error objects that represent the most recent errors that occurred during the current PowerShell session. Developers can access and view the details of these error objects to help troubleshoot and debug scripts.

How to catch specific exceptions in PowerShell?

To catch specific exceptions in PowerShell, you can use a try/catch block and specify the type of exception you want to catch. Here's an example:

try { $result = 1 / 0 # This will throw a DivideByZeroException } catch [System.DivideByZeroException] { Write-Host "Caught a DivideByZeroException: Cannot divide by zero." } catch { Write-Host "Caught an unexpected exception: $_.Exception.Message" }

In this example, the try block attempts to divide by zero, which will throw a DivideByZeroException. The catch block then catches the specific DivideByZeroException and prints a custom error message. If there is any other type of exception thrown, the generic catch block will catch it and print the exception message.

You can also catch multiple specific exceptions by chaining catch blocks:

try { # Some code that may throw exceptions } catch [System.DivideByZeroException] { # Handle DivideByZeroException } catch [System.InvalidOperationException] { # Handle InvalidOperationException } catch { # Handle any other types of exceptions }

The recommended way to catch exceptions in PowerShell is to use a try-catch block. This allows you to specify a block of code to try, and if an exception occurs within that block, you can specify a catch block to handle the exception. Here is an example of how to use try-catch in PowerShell:

try { # Code that might throw an exception $result = Get-Item -Path "C:\NonExistentFile.txt" } catch { # Handle the exception Write-Error "An error occurred: $_" }

In this example, the Get-Item cmdlet is attempting to retrieve information about a file that does not exist, which will throw an exception. The try block contains the code that may throw an exception, and the catch block contains the code to handle the exception, which in this case simply writes an error message to the console.

How to nest try-catch blocks in PowerShell?

In PowerShell, you can nest try-catch blocks by enclosing an inner try-catch block within an outer try block. Here's an example:

try { # Outer try block try { # Inner try block $result = 1 / 0 # This will cause a division by zero error } catch { Write-Host "Inner catch block: $_.Exception.Message" } } catch { Write-Host "Outer catch block: $_.Exception.Message" }

In this example, the inner try block attempts to perform a division by zero operation, which will trigger a divide by zero error. The inner catch block will catch this error and display an error message. If there is an error in the outer try block, the outer catch block will catch and handle it accordingly.

You can nest multiple try-catch blocks in PowerShell by replicating this structure as needed. Each inner try-catch block can handle specific exceptions or errors, and the outer catch block can serve as a catch-all for any unhandled exceptions.