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:
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:
[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:
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:
[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:
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:
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:
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:
[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:
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:
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
[adinserter name=”Matched-Content”]


Nice work, thanks!
Thanks for your feedback