Pattern Matching with the PowerShell Switch Operator

Switching to switch if I have multiple if’s

Words: 646

Time to read: ~ 3 minutes

Date of writing: 2017-10-19


Everybody has dealt with them in one form or another; Log Files, Captain’s Logs, Yule Logs, the list goes on!

In my case, we have logs on SQL Server Agent jobs; whether they’ve completed successfully or not, as well as any output created through the running of whatever is in the jobs.
Since these logs are mainly to do with Database Maintenance, we use them to identify indexes, statistics, backups, or DBCC CHECKDBs that are taking a particularly long time to maintain so we can hopefully do something about them.

We send out notification emails if jobs fail as well but hey, we’re DBAs, we like a backup!

Since this used to be done manually, and since I like PowerShell, I created separate functions that can take individual log files and parse out the needed data in a nice, easy format.

My problem, and the reason for this post, was figuring out, based on the name of the file in a directory, which function to call…


Let us create some strings so everyone can play along at home.

The files were split out into different .txt files bases on what was run, and I needed to invoke a particular function depending on what was in the file name.

Your strings may be different…

I’ve thrown that into a variable called $fileNames just to call them easier e.g. $filenames = 0..4 | ForEach-Object....

Since I don’t always know the exact string, I can’t use -eq, I have to rely on checking for parts of the string.

Simple Option

Now I could have just gone with the simple choice and used what I know. This would have been using multiple if / elseif statements with the -match operator.

This works…

It’s ALIVE!!!


…but it’s not brief and I’ve been learning about the switch operator so I want to use that.

What’s the point in learning about stuff if you’re not going to implement it, right?

That was the start of my problem…

When intuition proves me an idiot…

So I change the code away from the if / elseif code to a switch statement, added in a final clause in case I feck things up, and let it run…

Yep, I fecked things up…

Okay, that failed miserably. Let’s quickly glance at the help on the switch operator and see what we’re doing wrong i.e. Get-Help about_Switch -Full;

Regex! Great, lets do that!

If we throw a -regex before the value, we can check if it matches our string! Brilliant, let’s do that!

And it works! Brilliant!

I learned!

That’s not all folks!

Why I like blogging is because I learn stuff, arguably more stuff than just by working through the problem.

Such as I need to read the help files more rather than just skimming through them…

If you think about the above examples, something is wrong. It seems that you can do a lot more with if than you can do with switch.
This can’t be the case, otherwise why would Microsoft promote it when you have multiple if statements?

I must be missing something…

So I go back and read through the help files again, all of it…

It can accept an expression???

In my haste and false intuition, I assumed that switch couldn’t take an expression…all because my intuition said I had to wrap my expression in brackets.

It should have been curly brackets!!!

Which works!

All your base are belong to Switch

Final Round

Now in this case, I am happy with the switch -regex as it’s shorter and more compact but, if the time came where I need to check on multiple conditions or need to use an expression in the switch, it’s nice to know that I don’t have to revert to a whole heap of if / elseif/ else statements.

Author: Shane O'Neill

DBA, T-SQL and PowerShell admirer, Food, Coffee, Whiskey (not necessarily in that order)...

One thought on “Pattern Matching with the PowerShell Switch Operator”

What's your opinion?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s