Catch and Store Error Messages in PowerShell

I am a developer from Nebraska in the US.
Search for a command to run...

I am a developer from Nebraska in the US.
No comments yet. Be the first to comment.
If you have ever forked a repository on github, you have likely encountered the terms merge and rebase when researching how to interact with the original repository. Merging and rebasing are two different methods to solve the same problem, namely, ho...

If you use a personal access token to authenticate to github, and you should be using one, you may have encountered the below error when trying to push updates to a remote repository. (refusing to allow a Personal Access Token to create or update wor...

This short guide is intended to flatten the git learning curve for a beginner. The guide will cover the basics of getting started with git on the command line. Cloning a Repository Cloning a repository is the git term for downloading a remote project...

If you have ever installed a new library or language on your mac, you have probably experienced the frustration of receiving “Command Not Found” when trying to use it in the terminal. This is especially challenging for beginners who may wonder what h...

When you are in a flow, nothing is worse than having to stop to deal with a console error message. It would be great if the errors could be handled by the script and packaged into helpful messages. Happily, PowerShell offers this capability to catch, store, and take action on error messages when they happen in your code.
Like any good thing, catching and storing errors will require some planning upfront. What type of errors could your script encounter? Are there any actions the script could take when an error has happened? What do you want to do with the error after it has occurred?
Once you have answered these questions, you can begin coding a solution to catch and store any errors. The starting point for your script will be the PowerShell try, catch functionality. The try block will be the action taken in your script, and the catch block will be used if an error is encountered during the try action. Different error types can be specified and stored in variables in the catch block. The last error message that was encountered by the script is stored by default in $_ and can be stored in a variable. The example below stores the error in the $LastException variable.
Additionally, you can include multiple catch blocks, to handle specific types of errors and then a general catch block for every other error. The example below looks for an ItemNotFoundException and stores the error in an $ItemNotFound variable.
try
{
Write-Host "Here are your action steps."
}
#This will catch an Item Not Found Exception
catch [System.Management.Automation.ItemNotFoundException] {
$_.Exception.GetType().Name
$ItemNotFound = $_.Exception.GetType().Name
}
catch
{
Write-Host "Error: $($_)"
$LastException = $_
}
if ($LastException)
{
Write-Host "An error has occurred, the error text is $($LastException)."
}
These examples should give you a good start toward handling errors in your PowerShell scripts. There are many options for additional handling and adding catch blocks to find and store additional error messages. Thank you for reading!