PowerShell for Programmers: The Magic Switch!


Welcome back everyone, I'm trying out GitHub Gist for my code blocks this week. It lets you click and download them, as well as making them easy to edit. The downside is that I can't use my usual dark themed syntax highlighting. Let me know in the comments if you like gist or the old method better 🙂

Switch statements in PowerShell are very cool, and by knowing some of the tricks it has in place it can save you a lot of time.

Basic Syntax

Let's start by taking a look at a basic switch statement.

In C# you might write something like this:

In PowerShell you could do this:

Notice we don't use the Case: keyword at all.

The default in PowerShell is to assume -eq is used between your input and typed case value. This means No Case Sensitivity and No Wild Cards:

Expression Match

We have a long-hand switch statement that lets us use whatever we want (-eq, -gt, -le, -like, straight up boolean values, etc.)

Psudo-code:

Switch($value)
{
	{<bool expression>} {<code>}
	{<bool expression>} {<code>}
	default {<code>}
}

Real:

Jump Statements aren't necessary

C# requires jump statements such as break, goto, or return. PowerShell does not!

This is one of the coolest features in PowerShell. We actually allow for continuous case- checks.

This means your switches can actually act more like a bunch of independent if statements. Notice the previous example, without any of the "break statements" and using a number that is less than 5, 10 and 15.

How cool is that?

Very cool.

Loops and $_

It might be common for you to take a bunch of data, do a foreach loop through it and send each value through your switch:

However, PowerShell actually has a loop and $_ built right into your switch so we can chop off the foreach completely:

This lets us write some really concise and convenient little code blocks. The nice thing is that if our list has 1 object it still gets handled fine, and if it's an empty collection it will just skip the whole switch!

This, however, can lead to some confusion if you try to use "break" since our loop is also the whole switch statement:

Uh-oh, not good.

We also have "continue" in PowerShell and this will stop our current iteration of our loop (or switch) so we can use the looping feature and make it like a bunch of elseifs:

Shortcuts

In addition to the looping, we provide you a few other handy short cuts.

If you just wanted a basic equality switch, but would want to use -ceq (case sensitivity), -like (wild cards), or -match (regex) we let you do that without writing an expression match via some parameters.

Notice, weirdly, the parameters must come between the word "switch" and the parenthesis, they won't work at the end of the parenthesis.

Well that’s all for now, hopefully this helps you do some cool shortcuts with switches and get some nice, easy to read code into your scripts!

For the main series post, check back here.

If you find this helpful don't forget to rate, comment and share 🙂

Comments (4)

  1. MuratAKSARAY says:

    Thank you. It’s helpfull…

    1. Kory T says:

      Glad to help 🙂

  2. DJohnson1010 says:

    I had no idea that switch could handle a collection like that – thanks for that tip.

    1. Kory T says:

      No problem, it really is one of my favorite features and its the main reason I wanted to get this post written :). Glad you’re enjoying it!

Skip to main content