---
title: "Master PowerShell Try Catch Blocks with Examples"
description: "Demystify PowerShell try catch blocks with real-world examples. Learn error handling and improve your scripting skills and expertise."
canonical: "https://adamtheautomator.com/powershell-try-catch/"
---

# Master PowerShell Try Catch Blocks with Examples

> Demystify PowerShell try catch blocks with real-world examples. Learn error handling and improve your scripting skills and expertise.

Source: https://adamtheautomator.com/powershell-try-catch/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Master PowerShell Try Catch Blocks with Examples](https://adamtheautomator.com/wp-content/uploads/2020/03/ryan-grewell-PEvI6UOCEYE-unsplash-1.jpg)

# Master PowerShell Try Catch Blocks with Examples

[![](https://secure.gravatar.com/avatar/9a14f10ff1b1ec7d790d34f5b559e4d3de2d31b172e6ef266dfd8b479174d97b?s=192&d=mm&r=g)June Castillote](https://adamtheautomator.com/author/june/)24 March 20208 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Understanding How Errors Work in PowerShell](#understanding-how-errors-work-in-powershell)
*   [The $Error Automatic Variable](#the-error-automatic-variable)
*   [The $Error Object Properties](#the-error-object-properties)
*   [Terminating Errors](#terminating-errors)
*   [Non-Terminating Errors](#non-terminating-errors)
*   [The $ErrorActionPreference Variable](#the-erroractionpreference-variable)
*   [The ErrorAction Common Parameter](#the-erroraction-common-parameter)
*   [Using PowerShell Try Catch Blocks](#using-powershell-try-catch-blocks)
*   [Catching Non-Specific Errors (Catch-All) with PowerShell ErrorAction](#catching-non-specific-errors-catch-all-)
*   [Catching Specific Errors](#catching-specific-errors)
*   [Conclusion](#conclusion)
*   [Further Reading](#further-reading)

Have you ever run a script or a PowerShell cmdlet and get confronted with a screaming wall of text – in red – like the one shown below?

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

![Example of errors in PowerShell](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135128.044-1024x365.png)

Example of errors in PowerShell

Errors can become overwhelming and confusing. And most of all, errors are often hard to read, which makes determining what and where the script went wrong near impossible.

Luckily, you have some options in [PowerShell](https://adamtheautomator.com/tag/powershell/) to make this better through error handling. Using error handling, errors can be filtered and displayed in such a way that it’s easier to make sense of. And, understanding the error makes it easy to add more logic to error handling.

In this article, you will learn about errors in PowerShell and how they can be intercepted to perform error handling using the PowerShell `Try Catch` blocks (and `finally` blocks).

## Understanding How Errors Work in PowerShell

Before diving into error handling, let’s first cover a few concepts around errors in PowerShell. Understanding errors can lead to better error handling strategies.

### The `$Error` Automatic Variable

In PowerShell, there are a lot of _[automatic variables](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables)_, and one of them is the `$Error` automatic variable. PowerShell uses the `$Error` variable to store all errors that are encountered in the session. The `$Error` variable is an array of errors sorted by most recent.

When you first open a PowerShell session, the `$Error` variable is empty. You can check it so by calling the `$Error` variable.

![The $Error variable is empty](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135137.450.png)

The $Error variable is empty

As you can see, the `$Error` variable starts off empty. But, once an error is generated, the error will be added and stored into the `$Error` variable.

In the example below, the error is generated by deliberately getting a service name that does not exist.

```powershell
PS> Get-Service xyz
PS> $Error
PS> $Error.Count
```

![The error is added to the $Error variable](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135148.290.png)

The error is added to the $Error variable

As you can see from the output above, the generated error was added to the `$Error` variable.

> _The $Error variable contains a collection of errors generated in the PowerShell session. Each error can be access by calling its array position. The latest error will always be at index 0._
> 
> _For example, the latest error can be retrieved using `$Error[0]`._

### The `$Error` Object Properties

Since _[everything in PowerShell is an object](https://adamtheautomator.com/powershell-objects/)_, the `$Error` variable is an object, and objects have properties. By piping the `$Error` variable to the `Get-Member` cmdlet, you should see the list of the properties available.

```powershell
$Error | Get-Member
```

![The $Error object properties](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135157.998-1024x367.png)

The $Error object properties

To determine the reason for the error, you can view the content of the `InvocationInfo` property using the command below.

```powershell
$Error[0].InvocationInfo
```

![The InvocationInfo property](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135206.048.png)

The InvocationInfo property

Now, you could do the same with the other properties and discover what other information you can find!

### Terminating Errors

[Terminating errors](https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/terminating-errors) stop the execution flow when it is encountered by PowerShell vs non-terminating errors. There are several ways a terminating error can occur. One example is when you call a cmdlet with a parameter that does not exist.

As you’ll from the screenshot below, when the command `[Get-Process](https://adamtheautomator.com/powershell-get-process/ "Get-Process") notepad` runs, the command is valid, and the details of the _notepad_ process are displayed.

![The notepad process details](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135214.530.png)

The notepad process details

But, when a parameter that does not exist is used like `Get-Process notepad -handle 251`, the cmdlet displays an error that the `handle` parameter is not valid. Then, the cmdlet exits without showing the details of the `notepad` process.

![Error is thrown because the parameter is invalid](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135224.066.png)

Error is thrown because the parameter is invalid

### Non-Terminating Errors

Non-terminating errors are errors that do not stop the execution of the script or command. For example, check out the code below. This code gets the list of file names from the _fileslist.txt_ file. Then, the script goes through each file name, read the contents of each file, and outputs it on the screen.

```powershell
$file_list =  Get-Content .\filelist.txt
foreach ($file in $file_list) {
    Write-Output "Reading file $file"
    Get-Content $file
}
```

The contents of the _filelist.txt_ file are the file names shows in the list below.

```powershell
File_1.log
File_2.log
File_3.log
File_4.log
File_5.log
File_6.log
File_7.log
File_8.log
File_9.log
File_10.log
```

But what if _File\_6.log_ didn’t actually exist? When you run the code, you’d expect an error will happen because the script cannot find the _File\_6.log. Y_ou’ll see a similar output shown below.

![Example of non-terminating error](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135232.764-1024x517.png)

Example of non-terminating error

As you can see from the screenshot of the result above, the script was able to read the first five files in the list, but when it tried to read the file _File\_6.txt,_ an error is returned. The script then continued to read the rest of the files before exiting. It did not _terminate_.

### The `$ErrorActionPreference` Variable

So far, you’ve learned about the terminating and non-terminating errors and how they differ from each other. But, did you know that a non-terminating error can be forced to be treated as a terminating error?

PowerShell has a concept called p[_reference variables_](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables). These variables are used to change how PowerShell behaves many different ways. One of these variables is called `$ErrorActionPreference`.

The `$ErrorActionPreference` variable is used to change the way PowerShell treats non-terminating errors. By default, the `$ErrorActionPreference` value is set to `Continue`. Changing the value of the `$ErrorActionPreference` variable to `STOP`forces PowerShell to treat all errors as terminating errors.

Use the code below to change the `$ErrorActionPreference` value.

```powershell
$ErrorActionPreference = "STOP"
```

> To learn more about other valid $ErrorActionPreference variable values, visit [PowerShell ErrorActionPreference](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7#erroractionpreference).

Now, refer back to the example used in the **_Non-Terminating Errors_** section in this article. The script can modified to include the change in `$ErrorActionPreference` like the code shown below:

```powershell
# Set the $ErrorActionPreference value to STOP
$ErrorActionPreference = "STOP"
$file_list =  Get-Content .\filelist.txt
foreach ($file in $file_list) {
    Write-Output "Reading file $file"
    Get-Content $file
}
```

Running the modified code above will behave differently than before when the `$ErrorActionPreference` value is set to the default value of `Continue`.

![Forcing a terminating error using the $ErrorActionPreference variable](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135243.611-1024x389.png)

Forcing a terminating error using the `$ErrorActionPreference` variable

As you can see from the screenshot of the result above, the script was able to read the first five files in the list, but when it tried to read the file _File\_6.txt,_ an error is returned because the file was not found. Then, the script terminated, and the rest of the files are not read.

> _The `$ErrorActionPreference` value is only valid in the current PowerShell session. It resets to the default value once a new PowerShell session is started._

### The `ErrorAction` Common Parameter

If the `$ErrorActionPreference` value is applied to the PowerShell session, the `ErrorAction` parameter applies to any cmdlet that supports [_common parameters_](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7). The `ErrorAction` parameter accepts the same values that the `$ErrorActionPreference` variable does.

_The_ `ErrorAction` _parameter value takes precedence over the_ `$ErrorActionPreference` _value._

Let’s go back and use the same code in the previous example. But, this time, the `ErrorAction` parameter is added to the `Get-Content` line.

```powershell
# Set the $ErrorActionPreference value to default (CONTINUE)
$ErrorActionPreference = "CONTINUE"
$file_list =  Get-Content .\filelist.txt
foreach ($file in $file_list) {
    Write-Output "Reading file $file"
		# Use the -ErrorAction common parameter
		Get-Content $file -ErrorAction STOP
}
```

After running the modified code, you’ll see that even though the `$ErrorActionPreference` is set to `Continue`, the script still terminated once it encountered an error. The script terminated because the PowerShell `ErrorAction` parameter value in `Get-Content` is set to `STOP`.

![Forcing a terminating error using the ErrorAction parameter](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135255.384-1024x382.png)

Forcing a terminating error using the PowerShell `ErrorAction` parameter

## Using PowerShell Try Catch Blocks

At this point, you’ve learned about PowerShell errors and how the `$ErrorActionPreference` variable and PowerShell `ErrorAction` parameters work. Now, it’s time you learn about the good stuff – the PowerShell `Try Catch Finally` blocks.

PowerShell `try catch` blocks (and optional `finally block`) are a way to cast a net around a piece of code and catch any errors that return.

The code below shows the syntax of the `Try` statement.

```powershell
try {
    <statement list>
}
catch [[<error type>][',' <error type>]*]{
    <statement list>
}
finally {
    <statement list>
}
```

The `Try` block contains the code that you want PowerShell to “try” and monitor for errors. If the code in the `Try` block encounters an error, the error is added to the `$Error` variable and then passed to the `Catch` block.

The `Catch` block contains the actions to execute when it receives an error from the `Try` block. There can be multiple `Catch` blocks in a `Try` statement.

The `Finally` block contains that code that will at the end of the `Try` statement. This block runs whether or not an error was uncounted.

### Catching Non-Specific Errors (Catch-All) with PowerShell ErrorAction

A simple `Try` statement contains a `Try` and a `Catch` block. The `Finally` block is optional.

For example, to catch a non-specific exception, the `Catch` parameter should be empty. The example code below is using the same script that was used in the **The $ErrorActionPreference Variable** section but modified to use the `Try Catch` blocks.

As you can see from the code below, this time, the [`foreach` statement](https://adamtheautomator.com/powershell-foreach/) is enclosed inside the `Try` block. Then, a `Catch` block contains the code to display the string `An Error Occurred` if an error happened. The code in the `Finally` block just clears the `$Error` variable.

```powershell
$file_list = Get-Content .\filelist.txt
try {
    foreach ($file in $file_list) {
        Write-Output "Reading file $file"
        Get-Content $file -ErrorAction STOP
    }
}
catch {
    Write-Host "An Error Occured" -ForegroundColor RED
}
finally {
    $Error.Clear()
}
```

The code above, after running in PowerShell, will give you this output shown below.

![Script terminated when an error occurred](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135307.229.png)

Script terminated when an error occurred

The output above shows that the script encountered an error, ran the code inside the `Catch` block, and then terminated.

The error was handled, which was the point of error handling. However, the error displayed was too generic. To show a more descriptive error, you could access the `Exception` property of the error that was passed by the `Try` block.

The code below is modified, specifically the code inside the `Catch` block, to display the exception message from the current error that was passed down the pipeline –  `$PSItem.Exception.Message`

```powershell
$file_list = Get-Content .\filelist.txt
try {
    foreach ($file in $file_list) {
        Write-Output "Reading file $file"
        Get-Content $file -ErrorAction STOP
    }
}
catch {
    Write-Host $PSItem.Exception.Message -ForegroundColor RED
}
finally {
    $Error.Clear()
}
```

This time, when the modified code above is run, the message displayed is a lot more descriptive.

![Script terminated with a descriptive error message](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135319.403.png)

Script terminated with a descriptive error message

### Catching Specific Errors

There are times when a catch-all error handling is not the most appropriate approach. Perhaps, you want your script to perform an action that is dependent on the type of error that is encountered.

How do you determine the error type? By checking the `TypeName` value of the `Exception` property of the last error. For example, to find the error type from the previous example, use this command:

```powershell
$Error[0].Exception | Get-Member
```

The result of the code above would look like the screenshot below. As you can see, the `TypeName` value is displayed – `System.Management.Automation.ItemNotFoundException`.

![Getting the error TypeName value](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135328.892-1024x453.png)

Getting the error TypeName value

Now that you know the error type that you need to intercept, modify the code to catch it specifically. As you see from the modified code below, there are now two `Catch` blocks. The first `Catch` block intercepts a specific type of error (`System.Management.Automation.ItemNotFoundException`). In contrast, the second `Catch` block contains the generic, catch-all error message.

```powershell
$file_list = Get-Content .\filelist.txt
try {
    foreach ($file in $file_list) {
        Write-Output "Reading file $file"
        Get-Content $file -ErrorAction STOP
    }
}
catch [System.Management.Automation.ItemNotFoundException]{
    Write-Host "The file $file is not found." -ForegroundColor RED
}
catch {
    Write-Host $PSItem.Exception.Message -ForegroundColor RED
}
finally {
    $Error.Clear()
}
```

The screenshot below shows the output of the modified code above.

![Script terminated with a specific error message](https://adamtheautomator.com/wp-content/uploads/2020/05/Untitled-2020-05-27T135339.828.png)

Script terminated with a specific error message

## Conclusion

In this article, you’ve learned about errors in PowerShell, its properties, and how you can determine an error’s specific type. You’ve also learned the difference between how the `$ErrorActionPreference` variable and the PowerShell `ErrorAction` parameter affects how PowerShell treats non-terminating errors.

You have also learned how to use the PowerShell `Try Catch Finally` blocks to perform error handling, whether for specific errors or a catch-all approach.

The examples that are shown in this article only demonstrates the basics of how the `Try Catch Finally` blocks work. The knowledge that I hope you have gained in this article should give you the starting blocks to start applying error handling in your scripts.

## Further Reading

*   **_[About\_Try\_Catch\_Finally](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-7)_**
*   **_[About\_Automatic\_Variables](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7)_**
*   **_[Back to Basics: The PowerShell foreach Loop](https://adamtheautomator.com/powershell-foreach/)_**
*   **_[Back to Basics: Understanding PowerShell Objects](https://adamtheautomator.com/powershell-objects/)_**

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-try-catch%2F&text=Master%20PowerShell%20Try%20Catch%20Blocks%20with%20Examples)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-try-catch%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-try-catch%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
