How traveling help understand error handling in Powershell

How traveling help understand error handling in Powershell

Hi, Damian Garbus here. Last a few days I was thinking a little about error handling of Powershell script. I was reflecting on how many times are you using “try{} catch{}” block in your scripts? Doesn’t matter if it’s 10 lines script or more. How often are you want to control your script fails? This article is about basics error handling in Powershell especially for scripts run from Task Scheduler.

But first…

If you don’t know anything about error handling in Powershell, let me explain. It’s few lines of code more to help you catch error from the script. If you invoke the script manually, you can see the error in real time in Powershell console, but if your script is running from Task Scheduler, you can’t go back in time and look more information of error. To see what happened you can log everything to a text file from script and read this when you see that your script fail.

Try-catch block

It’s simple.

Try {
	"Do something"
catch {
	"Get errors from “try” block and save it to “$_” variable"

More You will show in examples.

It’s a good time to remember you that in the Blog Resources you can find Powershell Script Template for Task Scheduler. Function to write in the log file is included in this template. Next examples of error handling I will show you based on this script template.

Error handling in Powershell: Real-Life Examples

As you know, I explained some topics on this blog with real-life examples. On the Internet, I haven’t seen a real-life example for “try{} catch{}” block so let’s try to do this.


Let’s imagine that now you are in Paris and you would like to travel by car to two city of the Europe, eg. London and Moscow. One full fuel tank will be enough to go to London. If you want to go from Paris to Moscow, it’s not enough.

First example

First I will show you example with going to London. There are no errors, so the script finished well.

error handling in Powershell
error handling in Powershell

Second Example

In the second example, I would like to show you the way from Paris to Moscow. There you can see an error telling you about empty Fuel Tank. As you can see the error is displayed in the Powershell console but not in the log file.

error handling in Powershell

Third example

Now it’s time to use error handling and save error to a log file. Let’s do this.

error handling in Powershell
error handling in Powershell


It’s a too short article for describing all error handling topic. However, I don’t want to scare you, and now I showed the basics. If you search for more information about error handling, I recommend for you, in my opinion, one of the best articles on the Internet about this – PowerShell: Everything you wanted to know about exceptions. You will find more details.

Get Access to Poshland Resources for FREE!!!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailerLite ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.