• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Trap

PowerShell Trap

02/10/2018 by Stephanos 2 Comments

PowerShell Trap

In this tutorial we will see about PowerShell Trap and how we are able to use it.

When there is a terminating error, any statement will stop running. When we are not handling the terminating errors, PowerShell will stop running the function or script in the current pipeline. By using the Trap keyword, we are able to specify a list of statements when a terminating error will occur. Trap statements handle the terminating errors and allow execution of the script or function to continue instead of stopping. Trap statements will run when the terminating error will occur. 

Below is the syntax of PowerShell Trap Keyword:

trap [[<error type>]] {<statement list>}

As a basic Trap statement will run on any terminating that will occur. You are able to use Trap keyword multiple times in a function or script, by specifying the error type on each one. An error type requires brackets. Trap statements can appear anywhere in the script or command.

[adinserter name=”In Article”]

Trap for all terminating errors

As we have mentioned before, there are various methods to handle terminating errors.  If you are no using any other method to handle a terminating error, PowerShell checks for a Trap statement to handle the error and continue the script according to the Trap statement. See the below example for a simple Trap statement that will handle any error that will occur in the script.

Example 1

Code:

function Get-Eth {
    Trap {Write-Output "There is a terminating error."}
    Get-NetAdapters
}

Output:

PowerShell Trap - Example 1

Example 2

You are also able to display the error that triggered the trap by using $_.

Code:

function Get-Eth {
    Trap {Write-Output "There is a terminating error: $_"}
    Get-NetAdapters
}

Output:

PowerShell Trap - Example 2

[adinserter name=”In Article”]

Trap for specific terminating errors

By using the error type on a a trap statement, you are able to use specify the actions to be taken and the output to be shown for each type of error.

Example 3

Code:

function Get-Eth {
    Trap {Write-Output "Uknown terminating error."}
    Trap [System.Management.Automation.CommandNotFoundException]{
        Write-Output "There is a terminating error: $_"}
    Get-NetAdapters
}

Output:

PowerShell Trap - Example 3

 

Example 4

Code:

function Get-Eth {
    Trap {Write-Output "Uknown terminating error."}
    Trap [System.Management.Automation.CommandNotFoundException]{
        Write-Output "There is a terminating error: $_"}
    Get-NetAdapter -Physicals
}

Output:

PowerShell Trap - Example 4

[adinserter name=”In Article”]

Trap errors based on scope

when there is a terminating error and a Trap statement in the same scope, PowerShell will continue to the next statement in the that scope. If the Trap statement is on a different scope from where the terminating error occurred, PowerShell will continue to run the statements in the same scope of the Trap. Lets see the below examples to understand this better.

Example 5

Code:

function Get-Eth {
    Trap {Write-Output "Uknown terminating error."}
    Trap [System.Management.Automation.CommandNotFoundException]{
        Write-Output "There is a terminating error: $_"}
    Get-NetAdapters
    Write-Output "This is the END of the FUNCTION"
}
Write-Output "Script started processing..."
Write-Output "Processing..."
Write-Output "More processing..."
Get-Eth
Write-Output "This is the END of the SCRIPT"

Output:

PowerShell Trap - Example 5

As you can see in the output above, PowerShell continued to run the function until the end.

Example 6

Code:

function Get-Eth {
    Get-NetAdapters
    Write-Output "This is the END of the FUNCTION"
}
Trap {Write-Output "Uknown terminating error."}
Trap [System.Management.Automation.CommandNotFoundException]{
    Write-Output "There is a terminating error: $_"}
Write-Output "Script started processing..."
Write-Output "Processing..."
Write-Output "More processing..."
Get-Eth
Write-Output "This is the END of the SCRIPT"

Output:

PowerShell Trap - Example 6

As you can see in the output above, PowerShell continued to run the statements after the Trap statement even though the error occurred in the function.

[adinserter name=”In Article”]

Trap in combination with Break and Continue

By using Break and Continue keywords in a Trap statement, you are able to determine whether a function or script needs to be stopped or continued after the terminating error. Check the below examples to see how you are able to use them.

Example 7

In this example we are using Break keyword.

Code:

function Get-Eth {
    Trap {Write-Output "Uknown terminating error."}
    Trap [System.Management.Automation.CommandNotFoundException]{
        Write-Output "There is a terminating error: $_"
        Break}
    Get-NetAdapters
    Write-Output "This is the END of the FUNCTION"
}
Write-Output "Script started processing..."
Write-Output "Processing..."
Write-Output "More processing..."
Get-Eth
Write-Output "This is the END of the SCRIPT"

Output:

PowerShell Trap - Example 7

Example 8

Code:

function Get-Eth {
    Trap {Write-Output "Uknown terminating error."}
    Trap [System.Management.Automation.CommandNotFoundException]{
        Write-Output "There is a terminating error: $_"
        Continue}
    Get-NetAdapters
    Write-Output "This is the END of the FUNCTION"
}
Write-Output "Script started processing..."
Write-Output "Processing..."
Write-Output "More processing..."
Get-Eth
Write-Output "This is the END of the SCRIPT"

Output:

PowerShell Trap - Example 8

[adinserter name=”In Article”]

Example 9

Code:

function Get-Eth {
    Get-NetAdapters
    Write-Output "This is the END of the FUNCTION"
}
Trap {Write-Output "Uknown terminating error."}
Trap [System.Management.Automation.CommandNotFoundException]{
    Write-Output "There is a terminating error: $_"
    Break}
Write-Output "Script started processing..."
Write-Output "Processing..."
Write-Output "More processing..."
Get-Eth
Write-Output "This is the END of the SCRIPT"

Output:

PowerShell Trap - Example 9

Example 10

Code:

function Get-Eth {
    Get-NetAdapters
    Write-Output "This is the END of the FUNCTION"
}
Trap {Write-Output "Uknown terminating error."}
Trap [System.Management.Automation.CommandNotFoundException]{
    Write-Output "There is a terminating error: $_"
    Continue}
Write-Output "Script started processing..."
Write-Output "Processing..."
Write-Output "More processing..."
Get-Eth
Write-Output "This is the END of the SCRIPT"

Output:

PowerShell Trap - Example 10

I hope the tutorial about PowerShell Trap is helpful.

Please let me know your comments and thoughts.

You feedback is appreciated.

[adinserter name=”In Article”]

Related Links

  • PowerShell Tutorials
  • PowerShell Scripts
  • about_Trap | Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
PowerShell Trap
Article Name
PowerShell Trap
Description
PowerShell Trap. In this tutorial you will find information about PowerShell Trap and their use. Stephanos Constantinou Blog
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: Functions, Get-NetAdapter, PowerShell Break, PowerShell Continue, PowerShell Keywords, Write-Output

Reader Interactions

Comments

  1. Marian says

    08/10/2018 at 23:17

    Nice work, thanks!

    Reply
    • Stephanos says

      09/10/2018 at 08:58

      Thanks for your feedback

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact