1
00:00:00,840 --> 00:00:03,620
Another option that we have within PowerShell is what's

2
00:00:03,620 --> 00:00:07,140
referred to as a switch statement.

3
00:00:07,140 --> 00:00:09,740
So what are switch statements?

4
00:00:09,740 --> 00:00:09,940
Well,

5
00:00:09,940 --> 00:00:12,720
they are an alternate syntax for doing multiple

6
00:00:12,720 --> 00:00:17,340
comparisons with a single or multiple values.

7
00:00:17,340 --> 00:00:21,480
The result of an expression will then get compared into each

8
00:00:21,480 --> 00:00:24,840
of those switch statements or conditions.

9
00:00:24,840 --> 00:00:31,640
If a value matches, then the matching code block can then be executed.

10
00:00:31,640 --> 00:00:35,700
Now the statement itself, there's a default statement,

11
00:00:35,700 --> 00:00:38,580
which is the optional dropout. So let's go back and

12
00:00:38,580 --> 00:00:40,230
talk about if and else for a second.

13
00:00:40,230 --> 00:00:44,380
If we have an if statement with an else, that else is the default.

14
00:00:44,380 --> 00:00:49,290
A switch statement also has an equivalent of that else option,

15
00:00:49,290 --> 00:00:50,830
which is the default dropout.

16
00:00:50,830 --> 00:00:55,340
So if nothing matches, it always goes to that default statement.

17
00:00:55,340 --> 00:01:00,360
The test expression can be a logical or an integer expression because

18
00:01:00,360 --> 00:01:03,120
obviously we're matching a value against something. And then,

19
00:01:03,120 --> 00:01:03,500
of course,

20
00:01:03,500 --> 00:01:07,070
we can use break statements at any point to basically

21
00:01:07,070 --> 00:01:10,590
terminate any of the code execution, and that's actually quite common.

22
00:01:10,590 --> 00:01:11,480
So, for example,

23
00:01:11,480 --> 00:01:15,180
if we have a switch statement with six different values to match against

24
00:01:15,180 --> 00:01:18,020
one of those matches, then we break the statement,

25
00:01:18,020 --> 00:01:21,090
which means we come out of that switch loop and then we

26
00:01:21,090 --> 00:01:24,340
continue on with the rest of our execution.

27
00:01:24,340 --> 00:01:27,690
Now the switch statement contains specific components.

28
00:01:27,690 --> 00:01:30,340
The first one is the initial test expression.

29
00:01:30,340 --> 00:01:32,480
So what is it that we're testing for?

30
00:01:32,480 --> 00:01:35,810
What's the output? Is it a number?

31
00:01:35,810 --> 00:01:40,220
Is it a value that's being calculated, something else? Then,

32
00:01:40,220 --> 00:01:44,440
of course, underneath that, we then have conditions that we need to match,

33
00:01:44,440 --> 00:01:47,540
which is the switch statements themselves.

34
00:01:47,540 --> 00:01:50,160
Now the syntax for this looks very straightforward.

35
00:01:50,160 --> 00:01:54,500
You can see it starts with the word switch, and the expression is

36
00:01:54,500 --> 00:01:56,960
the evaluation that we're trying to get to.

37
00:01:56,960 --> 00:02:00,190
Then underneath that, we have the list of conditions,

38
00:02:00,190 --> 00:02:03,240
the ones that we're trying to match.

39
00:02:03,240 --> 00:02:06,390
So if we were to write this out as actual PowerShell,

40
00:02:06,390 --> 00:02:09,460
we would have a number of variable where it's equal to 3.

41
00:02:09,460 --> 00:02:12,640
So that gives me my variable that I wish to test.

42
00:02:12,640 --> 00:02:16,540
And then my switch statement is going to take that number.

43
00:02:16,540 --> 00:02:20,590
Now what's different here is in the switch option, you can see that

44
00:02:20,590 --> 00:02:22,970
we're using parentheses with the word number.

45
00:02:22,970 --> 00:02:25,750
Now in an if statement, that's going to match if that

46
00:02:25,750 --> 00:02:28,860
contains something. Whereas in the switch world,

47
00:02:28,860 --> 00:02:32,290
we're basically saying in this statement,

48
00:02:32,290 --> 00:02:36,240
that is the number or the object I wish to test.

49
00:02:36,240 --> 00:02:38,460
Then underneath it, you can see what looks like very

50
00:02:38,460 --> 00:02:41,560
strange kind of outlay where we have 5, 10,

51
00:02:41,560 --> 00:02:46,100
20, and default; 5 and 10 and 20 are the comparison

52
00:02:46,100 --> 00:02:48,300
operators or the values that we've chosen.

53
00:02:48,300 --> 00:02:54,030
So I want to see if the number 3 matches number 5, number 10, number

54
00:02:54,030 --> 00:02:56,750
20, which obviously, in this instance, it won't.

55
00:02:56,750 --> 00:03:01,840
It will then drop into the default option, which will then execute the code.

56
00:03:01,840 --> 00:03:05,930
So a switch statement is really a different way of doing multiple if

57
00:03:05,930 --> 00:03:12,390
and else/if statements. Now the switch statement syntax itself has a

58
00:03:12,390 --> 00:03:16,740
test expression, which can combine operators.

59
00:03:16,740 --> 00:03:19,770
It provides support for multiple expression tests.

60
00:03:19,770 --> 00:03:21,080
So if you're trying to check it,

61
00:03:21,080 --> 00:03:24,790
you can say check it against this and this. And then the output

62
00:03:24,790 --> 00:03:28,340
can be from multiple comparison code paths.

63
00:03:28,340 --> 00:03:30,140
So what does that really mean?

64
00:03:30,140 --> 00:03:33,190
Well, if we were to use multiple switch statements,

65
00:03:33,190 --> 00:03:35,240
the code would look like this.

66
00:03:35,240 --> 00:03:39,100
Let's say we have two expressions that we're trying to validate. So it says

67
00:03:39,100 --> 00:03:44,310
Switch evaluation1, evaluation2, and then our execution paths,

68
00:03:44,310 --> 00:03:45,740
which are our conditions.

69
00:03:45,740 --> 00:03:47,170
So what does that look like?

70
00:03:47,170 --> 00:03:52,410
So let's say we have two variables, number 1, number 2, value of 5, value of 11.

71
00:03:52,410 --> 00:03:57,080
Go back to our same code. Now notice, the start of the switch

72
00:03:57,080 --> 00:04:01,950
statement now contains two variables. So we're now going to pass

73
00:04:01,950 --> 00:04:05,540
both of those values into the switch statement.

74
00:04:05,540 --> 00:04:09,810
Now what's different here compared to an if/else is an if and an

75
00:04:09,810 --> 00:04:12,320
else and else/if statement is basically saying,

76
00:04:12,320 --> 00:04:14,150
well, if this value did something,

77
00:04:14,150 --> 00:04:18,500
then do this. Whereas in the switch statement, we're able to pass

78
00:04:18,500 --> 00:04:21,790
in multiple expressions, and each of those expressions gets

79
00:04:21,790 --> 00:04:24,740
evaluated against the list of switches.

80
00:04:24,740 --> 00:04:28,370
So when I run it through, number 1 would go first,

81
00:04:28,370 --> 00:04:31,620
which is set to 5. So it would pass that line that

82
00:04:31,620 --> 00:04:34,140
says Write‑Host Number equals 5.

83
00:04:34,140 --> 00:04:43,000
Number 2 evaluation that would then run would drop out of all three into the default option and then write the message out.

