1
00:00:00,140 --> 00:00:03,840
So let's go back into the Powershell console and we'll review the

2
00:00:03,840 --> 00:00:07,220
switch statement format, and then we'll look at how we implement the

3
00:00:07,220 --> 00:00:12,940
switch statement within the PowerShell.

4
00:00:12,940 --> 00:00:15,970
Okay, so we just talked about switch statements,

5
00:00:15,970 --> 00:00:20,240
so it's important to understand and see kind of firsthand what this looks like.

6
00:00:20,240 --> 00:00:23,560
So what we'll do is we'll create a variable here called

7
00:00:23,560 --> 00:00:27,620
number and we'll assign it a value of 3. Just to make sure

8
00:00:27,620 --> 00:00:31,160
that works, if we do number, we should get the value of 3.

9
00:00:31,160 --> 00:00:34,890
Okay, now we're going to define our switch statement,

10
00:00:34,890 --> 00:00:39,300
which starts with a Switch, and then, of course,

11
00:00:39,300 --> 00:00:42,940
in parentheses there, it will be the evaluation.

12
00:00:42,940 --> 00:00:43,540
Then, of course,

13
00:00:43,540 --> 00:00:48,210
we have our script block which kind of then contains various

14
00:00:48,210 --> 00:00:50,960
other conditions that we want to access.

15
00:00:50,960 --> 00:00:52,200
So to start this,

16
00:00:52,200 --> 00:00:56,560
we're going to say switch, and what we want to do is we want to evaluate the

17
00:00:56,560 --> 00:01:00,640
variable that we created so we start with Switch number,

18
00:01:00,640 --> 00:01:04,240
and then I'm going to open my code block here.

19
00:01:04,240 --> 00:01:06,760
What I'm then going to do is define the values that I

20
00:01:06,760 --> 00:01:08,500
want to check that number against.

21
00:01:08,500 --> 00:01:13,480
So let's start with 5, and then for each condition, we also get a

22
00:01:13,480 --> 00:01:15,680
script block that we can do something with.

23
00:01:15,680 --> 00:01:20,340
So I'm going to say Write‑Host and 5.

24
00:01:20,340 --> 00:01:28,340
Then I'm going to go to the next 1, let's say we do 10 Write‑Host 10

25
00:01:28,340 --> 00:01:38,940
and then we'll do another one so 20 Write‑Host 20.

26
00:01:38,940 --> 00:01:39,560
Okay.

27
00:01:39,560 --> 00:01:42,850
And then what we can do is I'm just going to close that out and

28
00:01:42,850 --> 00:01:46,600
then we'll press Enter here, and notice I get nothing because

29
00:01:46,600 --> 00:01:50,110
number is equal to three so it doesn't meet five, it doesn't

30
00:01:50,110 --> 00:01:52,740
match 10, it doesn't match 20.

31
00:01:52,740 --> 00:01:56,040
So how do we get it to drop out into something else?

32
00:01:56,040 --> 00:01:58,960
So let's just clear this out and what we'll do is loop back

33
00:01:58,960 --> 00:02:01,430
through, so I'm going to say switch 5,

34
00:02:01,430 --> 00:02:06,390
10, 20, and this time I'm going to use a specially

35
00:02:06,390 --> 00:02:11,760
reserved word here called Default and I'm going to say

36
00:02:11,760 --> 00:02:16,040
Default instead and close that out.

37
00:02:16,040 --> 00:02:17,370
So now we have that.

38
00:02:17,370 --> 00:02:19,990
I'm going to do my last ending, and when I press Enter now,

39
00:02:19,990 --> 00:02:24,220
it should come back and say default because when it did the switch comparisons,

40
00:02:24,220 --> 00:02:26,680
it looked at five, didn't match, looked at 10,

41
00:02:26,680 --> 00:02:28,730
didn't match, looked at 20, and then just said,

42
00:02:28,730 --> 00:02:31,870
you know what, I'm going to drop it out into the default option,

43
00:02:31,870 --> 00:02:33,340
and that's what I'm going to write.

44
00:02:33,340 --> 00:02:35,190
So a switch statement is really,

45
00:02:35,190 --> 00:02:38,430
really straightforward, just a list of check this, check that, check this.

46
00:02:38,430 --> 00:02:41,520
Now let's make this a bit more complicated here,

47
00:02:41,520 --> 00:02:46,880
and what we'll do is we'll update or create a number1 value and we'll

48
00:02:46,880 --> 00:02:51,310
call it 5, and then what we'll do is create a number2 and we'll make

49
00:02:51,310 --> 00:02:56,160
that an 11, so 2 different numbers. Now what we want to do here is we

50
00:02:56,160 --> 00:03:01,160
want our switch statement to take both of those numbers as an option

51
00:03:01,160 --> 00:03:02,840
for validation.

52
00:03:02,840 --> 00:03:08,650
So I'm going to say number2, so the same thing we did before, but this time I

53
00:03:08,650 --> 00:03:14,540
want it to check number1 and number2, so for number 5 and number 11 against

54
00:03:14,540 --> 00:03:16,820
the options or the conditions that we assign.

55
00:03:16,820 --> 00:03:23,900
So 5, I'm going to say Write‑Host 5, then I'll say

56
00:03:23,900 --> 00:03:27,190
10, do the same thing as before,

57
00:03:27,190 --> 00:03:33,910
Write‑Host and then do 10, and then I'll do 20, and then we will also

58
00:03:33,910 --> 00:03:39,870
include the default option here as well just so we have a default path that

59
00:03:39,870 --> 00:03:44,680
we can drop it out to in the event that it doesn't match any of the numbers

60
00:03:44,680 --> 00:03:48,440
that we're kind of passing into it.

61
00:03:48,440 --> 00:03:50,080
Okay, so our last one here,

62
00:03:50,080 --> 00:03:54,180
I'm going to press, oops, I didn't want to press the square, and

63
00:03:54,180 --> 00:03:56,070
you'll notice I just typed the square bracket,

64
00:03:56,070 --> 00:03:58,470
which means I now have to kind of type this back

65
00:03:58,470 --> 00:04:01,310
again, but I can do this quickly, 5, 10,

66
00:04:01,310 --> 00:04:08,440
20, Default, squirrely bracket, and Enter.

67
00:04:08,440 --> 00:04:11,240
Now notice it returns me two values.

68
00:04:11,240 --> 00:04:11,850
Interestingly,

69
00:04:11,850 --> 00:04:13,920
how that works because it takes the first number,

70
00:04:13,920 --> 00:04:16,380
number1, and drops through each line, 5,

71
00:04:16,380 --> 00:04:16,660
10,

72
00:04:16,660 --> 00:04:21,410
20, Default and it returns number 5 because the first number matches and

73
00:04:21,410 --> 00:04:25,810
then the second number goes in and comes back as a default.

74
00:04:25,810 --> 00:04:26,520
Now, of course,

75
00:04:26,520 --> 00:04:31,790
what would that look like if we created a number 3 and set that to 20?

76
00:04:31,790 --> 00:04:35,380
So let's go back and quickly create my switch statement 5,

77
00:04:35,380 --> 00:04:42,740
10, 20, Default, close that out and Enter, and notice what happened?

78
00:04:42,740 --> 00:04:44,450
It just took the two numbers.

79
00:04:44,450 --> 00:04:44,710
Now,

80
00:04:44,710 --> 00:04:49,200
if we tweaked our statement a little bit and added that third

81
00:04:49,200 --> 00:04:57,040
one, I'll go back up to here, $number3,

82
00:04:57,040 --> 00:05:02,040
and then obviously added the rest of our command, so let's do 5,

83
00:05:02,040 --> 00:05:05,950
10, 20, Default and then close out.

84
00:05:05,950 --> 00:05:09,680
Now we end up with three outputs so a switch statement can

85
00:05:09,680 --> 00:05:14,150
take multiple inputs for validation and for checking. So

86
00:05:14,150 --> 00:05:15,480
it's really simple to use them.

87
00:05:15,480 --> 00:05:18,490
I like using switch statements, sometimes it's easier than

88
00:05:18,490 --> 00:05:20,730
writing lots of if and else statements,

89
00:05:20,730 --> 00:05:24,370
and it just means that I have predetermined values that I want to check

90
00:05:24,370 --> 00:05:26,880
against and it just makes life a little bit easier.

91
00:05:26,880 --> 00:05:28,550
It really only functions well.

92
00:05:28,550 --> 00:05:32,210
If you're outputting numbers or you're doing calculations and have

93
00:05:32,210 --> 00:05:35,680
numbers come back out. You can also do string values, but there

94
00:05:35,680 --> 00:05:40,000
are other ways of doing that instead, so highly recommend using a switch statement.

