So let's go back into the Powershell console and we'll review the
switch statement format, and then we'll look at how we implement the
switch statement within the PowerShell.
Okay, so we just talked about switch statements,
so it's important to understand and see kind of firsthand what this looks like.
So what we'll do is we'll create a variable here called
number and we'll assign it a value of 3. Just to make sure
that works, if we do number, we should get the value of 3.
Okay, now we're going to define our switch statement,
which starts with a Switch, and then, of course,
in parentheses there, it will be the evaluation.
Then, of course,
we have our script block which kind of then contains various
other conditions that we want to access.
So to start this,
we're going to say switch, and what we want to do is we want to evaluate the
variable that we created so we start with Switch number,
and then I'm going to open my code block here.
What I'm then going to do is define the values that I
want to check that number against.
So let's start with 5, and then for each condition, we also get a
script block that we can do something with.
So I'm going to say Write‑Host and 5.
Then I'm going to go to the next 1, let's say we do 10 Write‑Host 10
and then we'll do another one so 20 Write‑Host 20.
Okay.
And then what we can do is I'm just going to close that out and
then we'll press Enter here, and notice I get nothing because
number is equal to three so it doesn't meet five, it doesn't
match 10, it doesn't match 20.
So how do we get it to drop out into something else?
So let's just clear this out and what we'll do is loop back
through, so I'm going to say switch 5,
10, 20, and this time I'm going to use a specially
reserved word here called Default and I'm going to say
Default instead and close that out.
So now we have that.
I'm going to do my last ending, and when I press Enter now,
it should come back and say default because when it did the switch comparisons,
it looked at five, didn't match, looked at 10,
didn't match, looked at 20, and then just said,
you know what, I'm going to drop it out into the default option,
and that's what I'm going to write.
So a switch statement is really,
really straightforward, just a list of check this, check that, check this.
Now let's make this a bit more complicated here,
and what we'll do is we'll update or create a number1 value and we'll
call it 5, and then what we'll do is create a number2 and we'll make
that an 11, so 2 different numbers. Now what we want to do here is we
want our switch statement to take both of those numbers as an option
for validation.
So I'm going to say number2, so the same thing we did before, but this time I
want it to check number1 and number2, so for number 5 and number 11 against
the options or the conditions that we assign.
So 5, I'm going to say Write‑Host 5, then I'll say
10, do the same thing as before,
Write‑Host and then do 10, and then I'll do 20, and then we will also
include the default option here as well just so we have a default path that
we can drop it out to in the event that it doesn't match any of the numbers
that we're kind of passing into it.
Okay, so our last one here,
I'm going to press, oops, I didn't want to press the square, and
you'll notice I just typed the square bracket,
which means I now have to kind of type this back
again, but I can do this quickly, 5, 10,
20, Default, squirrely bracket, and Enter.
Now notice it returns me two values.
Interestingly,
how that works because it takes the first number,
number1, and drops through each line, 5,
10,
20, Default and it returns number 5 because the first number matches and
then the second number goes in and comes back as a default.
Now, of course,
what would that look like if we created a number 3 and set that to 20?
So let's go back and quickly create my switch statement 5,
10, 20, Default, close that out and Enter, and notice what happened?
It just took the two numbers.
Now,
if we tweaked our statement a little bit and added that third
one, I'll go back up to here, $number3,
and then obviously added the rest of our command, so let's do 5,
10, 20, Default and then close out.
Now we end up with three outputs so a switch statement can
take multiple inputs for validation and for checking. So
it's really simple to use them.
I like using switch statements, sometimes it's easier than
writing lots of if and else statements,
and it just means that I have predetermined values that I want to check
against and it just makes life a little bit easier.
It really only functions well.
If you're outputting numbers or you're doing calculations and have
numbers come back out. You can also do string values, but there
are other ways of doing that instead, so highly recommend using a switch statement.