Error Handling With PowerShell

If you are like me, not only have you never made an error in your script but all external forces that are out of your control also always work as intended! What do people without the magic PowerShell touch do? They embark in the ancient battle that has gone on for generations called error handling

Error Handling With PowerShell

Error handling with PowerShell helps ensure that an unexpected exception does not let a script continue to cause issues. Let’s take a look at a script that will make sure only cool people have access to all the cool stuff.

$GroupMembership = Get-ADGroupMember -Identity “Cool Users”

$UpdatedCoolList = Get-Content \\FileShare\Location\CoolPeople.csv
Foreach($Person in $GroupMembership){

If($UpdatedCoolList -notcontains $Person){

         Remove-ADGRoupMember -Identity "Cool Users" -User $Person


Works great! However, Kris Powell has found out about the cool list, and in anger at being left off the list, he performs an action that proves we are right to keep him out. He deletes CoolPeople.csv.

Next time the script runs, we get an exception:

Error handling with PowerShell

With a blank $UpdatedCoolList variable it removes everyone’s access. Very not cool Kris.


Before we go into handling the sabotage, it is important to learn about exceptions. An exception is an error or unexpected result that PowerShell needs you to handle.

There are two types of exceptions: terminating and non-terminating. Terminating exceptions stop the running script. Non-terminating exceptions just write to the error pipeline.


Making a non-terminating exception a terminating exception is simple. You can set the command’s parameter to -ErrorAction to stop.

$UpdatedCoolList = Get-Content \\FileShare\Location\CoolPeople.csv -ErrorAction Stop

You can also set the default action of all errors to stop by setting the variable $ErrorActionPreference to “stop”.

In most cases, a cmdlet generates a non-terminating exception, but error handling with PowerShell requires a terminating exception to work.


Once we have set the error action to stop we can wrap it in a Try/Catch block (more info). Try is where we run the command, Catch is what will run if it runs into a terminating error.

$UpdatedCoolList = Get-Content \\FileShare\Location\CoolPeople.csv -ErrorAction Stop
$_.Exception | Out-File C:\cool_log\krisdidit.log -Append

In this scriptblock, we captured the exception and put it in a log file. Break was used to exit the script so it would not continue. If we had left the catch block empty it would absorb the error and continue the script.

The last piece of the error handling with a Try/Catch/Finally block is the Finally. This is used to provide actions that will always run before exiting the script (or continuing on). This is mostly used for clean up. You can use it to close database connections, remove files, or close file locks.  Finally allows you to revert any changes done as a result of the script whether an exception is encountered or not.


That is it for the basics. Whenever you are dealing with elements that are volatile or out of your control, error handling is a great way to make sure that your script will not cause problems if things go wrong.

Your email address will not be published.

Your Name

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