Another option that we have within PowerShell is what's
referred to as a switch statement.
So what are switch statements?
Well,
they are an alternate syntax for doing multiple
comparisons with a single or multiple values.
The result of an expression will then get compared into each
of those switch statements or conditions.
If a value matches, then the matching code block can then be executed.
Now the statement itself, there's a default statement,
which is the optional dropout. So let's go back and
talk about if and else for a second.
If we have an if statement with an else, that else is the default.
A switch statement also has an equivalent of that else option,
which is the default dropout.
So if nothing matches, it always goes to that default statement.
The test expression can be a logical or an integer expression because
obviously we're matching a value against something. And then,
of course,
we can use break statements at any point to basically
terminate any of the code execution, and that's actually quite common.
So, for example,
if we have a switch statement with six different values to match against
one of those matches, then we break the statement,
which means we come out of that switch loop and then we
continue on with the rest of our execution.
Now the switch statement contains specific components.
The first one is the initial test expression.
So what is it that we're testing for?
What's the output? Is it a number?
Is it a value that's being calculated, something else? Then,
of course, underneath that, we then have conditions that we need to match,
which is the switch statements themselves.
Now the syntax for this looks very straightforward.
You can see it starts with the word switch, and the expression is
the evaluation that we're trying to get to.
Then underneath that, we have the list of conditions,
the ones that we're trying to match.
So if we were to write this out as actual PowerShell,
we would have a number of variable where it's equal to 3.
So that gives me my variable that I wish to test.
And then my switch statement is going to take that number.
Now what's different here is in the switch option, you can see that
we're using parentheses with the word number.
Now in an if statement, that's going to match if that
contains something. Whereas in the switch world,
we're basically saying in this statement,
that is the number or the object I wish to test.
Then underneath it, you can see what looks like very
strange kind of outlay where we have 5, 10,
20, and default; 5 and 10 and 20 are the comparison
operators or the values that we've chosen.
So I want to see if the number 3 matches number 5, number 10, number
20, which obviously, in this instance, it won't.
It will then drop into the default option, which will then execute the code.
So a switch statement is really a different way of doing multiple if
and else/if statements. Now the switch statement syntax itself has a
test expression, which can combine operators.
It provides support for multiple expression tests.
So if you're trying to check it,
you can say check it against this and this. And then the output
can be from multiple comparison code paths.
So what does that really mean?
Well, if we were to use multiple switch statements,
the code would look like this.
Let's say we have two expressions that we're trying to validate. So it says
Switch evaluation1, evaluation2, and then our execution paths,
which are our conditions.
So what does that look like?
So let's say we have two variables, number 1, number 2, value of 5, value of 11.
Go back to our same code. Now notice, the start of the switch
statement now contains two variables. So we're now going to pass
both of those values into the switch statement.
Now what's different here compared to an if/else is an if and an
else and else/if statement is basically saying,
well, if this value did something,
then do this. Whereas in the switch statement, we're able to pass
in multiple expressions, and each of those expressions gets
evaluated against the list of switches.
So when I run it through, number 1 would go first,
which is set to 5. So it would pass that line that
says Write‑Host Number equals 5.
Number 2 evaluation that would then run would drop out of all three into the default option and then write the message out.