1
00:00:00,240 --> 00:00:02,800
So let's go into our PowerShell console,

2
00:00:02,800 --> 00:00:06,540
and we'll first review how we can use the if/else statements,

3
00:00:06,540 --> 00:00:11,140
and then we'll implement some basic logic using if/else statements.

4
00:00:11,140 --> 00:00:13,800
So we're back in the PowerShell console.

5
00:00:13,800 --> 00:00:14,440
And, of course,

6
00:00:14,440 --> 00:00:18,040
what we want to do here is actually look at the if statement itself.

7
00:00:18,040 --> 00:00:21,810
So the first thing we'll do is actually just create a variable,

8
00:00:21,810 --> 00:00:26,700
and I'm just going to create it as an arbitrary variable here,

9
00:00:26,700 --> 00:00:28,980
which just basically says = "Value",

10
00:00:28,980 --> 00:00:31,580
and of course to check the value of that one,

11
00:00:31,580 --> 00:00:35,140
I can just do this and it will return that value for me.

12
00:00:35,140 --> 00:00:37,230
Now what I want to be able to do is now that I've got this

13
00:00:37,230 --> 00:00:40,280
variable is run it through an if statement to be able to

14
00:00:40,280 --> 00:00:43,540
check if a value is in there.

15
00:00:43,540 --> 00:00:47,440
So I can do this by saying if($variable),

16
00:00:47,440 --> 00:00:51,320
and then of course you can see we have our code block commands right here.

17
00:00:51,320 --> 00:00:55,240
This is where we type in the commands that we wish to run.

18
00:00:55,240 --> 00:00:57,360
So I'm going to say Write,

19
00:00:57,360 --> 00:01:09,460
let's just do Write‑Host and say the $variable returned, and I'll just say

20
00:01:09,460 --> 00:01:14,050
true, and then close that. And notice when I press Enter,

21
00:01:14,050 --> 00:01:17,300
it executes the code immediately because it's just

22
00:01:17,300 --> 00:01:19,480
a line that says if something.

23
00:01:19,480 --> 00:01:22,290
So we have some syntax now where it just says if the

24
00:01:22,290 --> 00:01:25,070
variable has a value of some description,

25
00:01:25,070 --> 00:01:28,420
we're not doing the evaluation of the value that's in there,

26
00:01:28,420 --> 00:01:31,100
whether it's a 1 or a 10 or a string value, we're

27
00:01:31,100 --> 00:01:34,150
literally just saying if it has something,

28
00:01:34,150 --> 00:01:36,200
then render that value out.

29
00:01:36,200 --> 00:01:39,410
So let's just clear that for a second and try this one again.

30
00:01:39,410 --> 00:01:44,440
And this time, what we'll do is we'll include an else statement.

31
00:01:44,440 --> 00:01:49,230
So what I can do here is you can see I'm writing it all out on one line.

32
00:01:49,230 --> 00:01:54,070
So what I'm going to do here is actually just kind of go here and press Enter,

33
00:01:54,070 --> 00:01:58,060
and you'll notice it gives me to kind of chevron arrows, that means I can

34
00:01:58,060 --> 00:02:02,020
continue writing my code in the next line down because,

35
00:02:02,020 --> 00:02:04,140
of course, as you're writing code,

36
00:02:04,140 --> 00:02:08,490
you need a little bit more space than just trying to write it on one line.

37
00:02:08,490 --> 00:02:13,520
So $variable check returned true,

38
00:02:13,520 --> 00:02:17,920
and then we'll do this, and then of course I can go here, I can

39
00:02:17,920 --> 00:02:23,220
then type the word else with my open bracket here, and then I'm

40
00:02:23,220 --> 00:02:34,740
going to say Write‑Host "The $variable check returned false",

41
00:02:34,740 --> 00:02:37,040
and then close that out.

42
00:02:37,040 --> 00:02:38,530
So that gives me my statement.

43
00:02:38,530 --> 00:02:41,580
Now, of course, you'll notice it's still kind of sitting there,

44
00:02:41,580 --> 00:02:44,250
and it doesn't really know what to do with it, so when you

45
00:02:44,250 --> 00:02:47,100
look at your code you need to double check that you've got

46
00:02:47,100 --> 00:02:48,640
everything in the right places.

47
00:02:48,640 --> 00:02:52,400
I'm going to press Enter, that will finish, and it will say The Value check

48
00:02:52,400 --> 00:02:56,640
returned true because, of course, it does, it has a value.

49
00:02:56,640 --> 00:03:03,130
So what I can do now is I can say $variable = null. Now,

50
00:03:03,130 --> 00:03:04,480
of course, notice what happened.

51
00:03:04,480 --> 00:03:09,310
It doesn't take that blank value or understand what the word null is, so

52
00:03:09,310 --> 00:03:13,420
if I now do $null and then say go and get me variable,

53
00:03:13,420 --> 00:03:15,500
you notice it has no value.

54
00:03:15,500 --> 00:03:18,870
So let me just clear this, and what we'll do is just loop

55
00:03:18,870 --> 00:03:24,060
through our commands and I should get my larger one that's here,

56
00:03:24,060 --> 00:03:28,540
and I can quickly write this value out.

57
00:03:28,540 --> 00:03:32,730
So if we just loop back through here,

58
00:03:32,730 --> 00:03:39,710
so clear if the variable write it true, else write it false. Oh,

59
00:03:39,710 --> 00:03:43,010
I don't need that one, I can just actually press Enter here. And then

60
00:03:43,010 --> 00:03:46,080
it will come back and say the variable has no value,

61
00:03:46,080 --> 00:03:49,540
so it doesn't write anything out. It now returns false.

62
00:03:49,540 --> 00:03:53,650
So this is literally the most basic if statement is just

63
00:03:53,650 --> 00:03:58,550
checking if the variable has a specific value.

64
00:03:58,550 --> 00:04:02,230
Now, of course, though that's useful, it's not always the best approach because,

65
00:04:02,230 --> 00:04:02,600
of course,

66
00:04:02,600 --> 00:04:07,460
what you want to be able to do is check if this value is equal to

67
00:04:07,460 --> 00:04:11,840
something or it's like that or it matches something else.

68
00:04:11,840 --> 00:04:13,180
But before we can do that,

69
00:04:13,180 --> 00:04:16,900
we also need to understand that we can have multiple comparisons too.

70
00:04:16,900 --> 00:04:25,540
So I'm going to create a new variable, and I'll assign it the number 1.

71
00:04:25,540 --> 00:04:31,110
I'll then do a variable2 2 and assign that number 2.

72
00:04:31,110 --> 00:04:34,140
So we have two variables, variable and variable2.

73
00:04:34,140 --> 00:04:38,120
What I can then do is do my same kind of if statements,

74
00:04:38,120 --> 00:04:40,130
and I'm just going to check if they have a value.

75
00:04:40,130 --> 00:04:45,530
So I'm going to say if($variable), remember I can then go to the statement and

76
00:04:45,530 --> 00:04:53,240
say Write‑Host, and then from here I'll just say $variable.

77
00:04:53,240 --> 00:04:55,890
So I'm just going to output the value. Then,

78
00:04:55,890 --> 00:04:56,510
of course,

79
00:04:56,510 --> 00:05:01,510
at the end of this one I'm going to close that code, kind of script

80
00:05:01,510 --> 00:05:04,330
block out, then I'm going to do something different.

81
00:05:04,330 --> 00:05:10,320
I'm going to say elseif, and then I'm going to check variable2. So

82
00:05:10,320 --> 00:05:17,970
variable2, there's my script block, Write‑Host. I'm then going to

83
00:05:17,970 --> 00:05:23,710
say $variable2, close that script block,

84
00:05:23,710 --> 00:05:26,740
and then I'm going to do an else statement,

85
00:05:26,740 --> 00:05:33,830
which will then just say Write‑Host "Anything", and

86
00:05:33,830 --> 00:05:36,140
then I'll close that one out too.

87
00:05:36,140 --> 00:05:38,160
Okay, now notice what it does.

88
00:05:38,160 --> 00:05:41,400
It passes the first test and it says yes,

89
00:05:41,400 --> 00:05:44,200
there's a value, and then it writes out the value because that's

90
00:05:44,200 --> 00:05:46,330
the first thing it matches because it says okay,

91
00:05:46,330 --> 00:05:49,440
this has a value of some description.

92
00:05:49,440 --> 00:05:50,040
Now,

93
00:05:50,040 --> 00:05:57,840
if we were to change that, if I said variable = $null, like so,

94
00:05:57,840 --> 00:06:00,800
I'm just going to clear that because we're going to have to tap

95
00:06:00,800 --> 00:06:02,640
through a couple of these again.

96
00:06:02,640 --> 00:06:03,900
So let me just leap up here.

97
00:06:03,900 --> 00:06:11,540
I'm going to say if($variable), Write‑Host, elseif, Write‑Host, else, Anything,

98
00:06:11,540 --> 00:06:17,020
it now goes to number 2 because the first one fails the test because it's a

99
00:06:17,020 --> 00:06:19,890
null entry, and so it then goes to the second one.

100
00:06:19,890 --> 00:06:25,390
If I was then to null out the second variable, it would drop out to anything.

101
00:06:25,390 --> 00:06:31,540
So we can do that too. So I can say variable2 = $null.

102
00:06:31,540 --> 00:06:34,240
Let's go back here and clear,

103
00:06:34,240 --> 00:06:38,300
and we'll just loop back up to if($variable), Write‑Host, else,

104
00:06:38,300 --> 00:06:39,750
Write‑Host, else,

105
00:06:39,750 --> 00:06:45,210
Write‑Host Anything, let me get rid of that last line, and Enter,

106
00:06:45,210 --> 00:06:46,300
and it should go to Anything.

107
00:06:46,300 --> 00:06:47,170
So as you can see,

108
00:06:47,170 --> 00:06:51,880
we've just forced it to drop down to each one. So variable, when it was

109
00:06:51,880 --> 00:06:55,760
correct and it was not null, it was a value of 1 so it passed.

110
00:06:55,760 --> 00:06:59,540
Then when that was null, it went to variable2, and then when that was null,

111
00:06:59,540 --> 00:07:03,340
it went to the default value, which is number 3.

112
00:07:03,340 --> 00:07:05,940
Now, of course, that works out really,

113
00:07:05,940 --> 00:07:08,920
really well in order for you to be able to compare and

114
00:07:08,920 --> 00:07:11,440
check whether a variable has a value.

115
00:07:11,440 --> 00:07:15,440
But like we talked about, you often want to compare things together.

116
00:07:15,440 --> 00:07:18,380
So, I'm going to create a new variable1,

117
00:07:18,380 --> 00:07:21,770
and I'll put the number 1 in that variable.

118
00:07:21,770 --> 00:07:25,880
I'll then go ahead and create variable2 and put the number 3

119
00:07:25,880 --> 00:07:29,140
in there. So we have to number values.

120
00:07:29,140 --> 00:07:32,810
Now what we're going to do here is look at how we compare those two together.

121
00:07:32,810 --> 00:07:39,240
So I'm going to say variable1, and then I'm going to use the eq operator.

122
00:07:39,240 --> 00:07:43,340
So let's do variable2.

123
00:07:43,340 --> 00:07:48,440
So if variable1, so number 1, equals number 3,

124
00:07:48,440 --> 00:07:49,450
then, of course,

125
00:07:49,450 --> 00:07:55,370
I'm going to write the host message here and say They are the

126
00:07:55,370 --> 00:07:58,300
same. And we'll leave it as that, I'm not going to add an else

127
00:07:58,300 --> 00:08:02,060
or an else/if, but when I run it, you can see what happens.

128
00:08:02,060 --> 00:08:03,990
It just says, well, if they're equal to something,

129
00:08:03,990 --> 00:08:07,670
do something, and because I don't have an else, which is the default dropout,

130
00:08:07,670 --> 00:08:10,440
it returns me no value whatsoever.

131
00:08:10,440 --> 00:08:13,180
Now, if we go back and change this and say,

132
00:08:13,180 --> 00:08:13,510
well,

133
00:08:13,510 --> 00:08:16,320
what about if we chose a different operator, such as less

134
00:08:16,320 --> 00:08:18,680
than, now I get a message back. Now, of course,

135
00:08:18,680 --> 00:08:22,060
the message makes no sense because I'm having it write They are the same, but

136
00:08:22,060 --> 00:08:27,730
if the variable was less than, or I could change it and say if it was greater

137
00:08:27,730 --> 00:08:30,440
than, you can see that we get different outputs.

138
00:08:30,440 --> 00:08:34,720
So this is the key in using if statements is that they can often

139
00:08:34,720 --> 00:08:39,460
output different values based on the kind of querying that we're

140
00:08:39,460 --> 00:08:42,130
doing together to be able to say, oh, hey,

141
00:08:42,130 --> 00:08:44,410
if this number is greater than that or less than this or

142
00:08:44,410 --> 00:08:46,570
this text value matches this value,

143
00:08:46,570 --> 00:08:52,040
etc. So we can do all kinds of different if statements and checks here.

144
00:08:52,040 --> 00:08:52,640
Now, of course,

145
00:08:52,640 --> 00:08:55,460
one of the big things that we can do is obviously

146
00:08:55,460 --> 00:08:58,090
creating variables that contain values.

147
00:08:58,090 --> 00:09:01,390
So I'm going to create a new variable here.

148
00:09:01,390 --> 00:09:06,520
I'm going to use 1..10, and then I'm going to say, well, what's in the array?

149
00:09:06,520 --> 00:09:11,300
And sure enough, you can see it's 1 to 10. So this creates me an array of values.

150
00:09:11,300 --> 00:09:15,660
What I'm also going to do is just create a new variable called compare,

151
00:09:15,660 --> 00:09:17,940
and I'll assign that to number 6.

152
00:09:17,940 --> 00:09:19,150
So let me just clear that.

153
00:09:19,150 --> 00:09:22,290
So let me just go back and say array and then

154
00:09:22,290 --> 00:09:26,240
$compare 6, so we have the two values.

155
00:09:26,240 --> 00:09:31,590
What I want to be able to do here is check if the current array or the

156
00:09:31,590 --> 00:09:35,600
array contains the number that I want to utilize.

157
00:09:35,600 --> 00:09:37,600
So I'm going to use an if statement again.

158
00:09:37,600 --> 00:09:44,360
So I'm going to say, if($array, and then what I can do is say ‑contains,

159
00:09:44,360 --> 00:09:47,950
which is a valid operator, and I'm going to say contains the

160
00:09:47,950 --> 00:09:57,000
$compare, then I'm just going to say Write‑Host "Compared." So

161
00:09:57,000 --> 00:09:59,540
let's press Enter, and of course, sure enough,

162
00:09:59,540 --> 00:10:00,220
it does.

163
00:10:00,220 --> 00:10:05,120
The array contains that specific number. Now we could flip this

164
00:10:05,120 --> 00:10:09,130
around the other way and do a different kind of validation here. So

165
00:10:09,130 --> 00:10:16,890
I can go back and say compare is in the array, and it will come

166
00:10:16,890 --> 00:10:18,940
back and give us the same value.

167
00:10:18,940 --> 00:10:21,410
Now, if we look at the syntax a little bit,

168
00:10:21,410 --> 00:10:25,190
and maybe I'm going to change from contains and say

169
00:10:25,190 --> 00:10:31,900
array is greater than compare, it will come back and say,

170
00:10:31,900 --> 00:10:34,600
well, yes, there's more in the array.

171
00:10:34,600 --> 00:10:37,870
It's a greater value than that that's in compare,

172
00:10:37,870 --> 00:10:39,510
which obviously works for numbers.

173
00:10:39,510 --> 00:10:40,930
If we're using other values,

174
00:10:40,930 --> 00:10:43,000
it becomes a little more complicated and you have to

175
00:10:43,000 --> 00:10:45,540
do other things to make that work.

176
00:10:45,540 --> 00:10:46,100
Now, of course,

177
00:10:46,100 --> 00:10:51,240
we could do simple calculations, we could do multiple calculations.

178
00:10:51,240 --> 00:10:56,360
So, let's say we wanted to use our same variables.

179
00:10:56,360 --> 00:10:57,590
So let's just double check.

180
00:10:57,590 --> 00:10:59,040
We have our array.

181
00:10:59,040 --> 00:11:02,810
So let's say we wanted to check if a value existed in something and

182
00:11:02,810 --> 00:11:05,740
then if a number was bigger than something else.

183
00:11:05,740 --> 00:11:16,140
So what we can do here is say if, let's do compare ‑in $array.

184
00:11:16,140 --> 00:11:18,350
So there's our first comparison statement.

185
00:11:18,350 --> 00:11:20,270
Now I'm going to do something different here.

186
00:11:20,270 --> 00:11:24,540
I'm going to go here and put double kind of quotes around it.

187
00:11:24,540 --> 00:11:26,900
But before I put the other one in there,

188
00:11:26,900 --> 00:11:30,060
I'm going to say ‑and, and then I'm going to close

189
00:11:30,060 --> 00:11:31,930
out. So you can see our syntax here.

190
00:11:31,930 --> 00:11:40,040
So if the current compare value is in the array and I'm going to say compare

191
00:11:40,040 --> 00:11:45,940
is greater than 4, I'm just using an arbitrary value here,

192
00:11:45,940 --> 00:11:48,420
then we're going to use our script block here,

193
00:11:48,420 --> 00:11:56,110
and I'll just say Write‑Host "Yes", and press Enter. Now notice it passes both.

194
00:11:56,110 --> 00:11:57,540
It will say, well yes,

195
00:11:57,540 --> 00:12:02,370
compare does exist in the array and then compare is greater than 4.

196
00:12:02,370 --> 00:12:06,960
Now, if I go back and change this to 8, we should get nothing,

197
00:12:06,960 --> 00:12:08,180
which is exactly right.

198
00:12:08,180 --> 00:12:09,470
And why do we get nothing?

199
00:12:09,470 --> 00:12:15,490
Because if we just go here, I didn't have an else output.

200
00:12:15,490 --> 00:12:22,640
So I'm going to say Write‑Host "No" and close that.

201
00:12:22,640 --> 00:12:27,740
And now we get a No. So if you don't utilize an else or else/if, you'll

202
00:12:27,740 --> 00:12:30,530
always just pass the first one and then nothing gets passed,

203
00:12:30,530 --> 00:12:31,860
you'll get no value,

204
00:12:31,860 --> 00:12:35,520
no nothing, it just kind of stops that's there. So

205
00:12:35,520 --> 00:12:37,350
you can see we can do a comparison.

206
00:12:37,350 --> 00:12:38,640
I'm just going to clear this again,

207
00:12:38,640 --> 00:12:45,040
and what we'll do is I'll just change the if one here, and this time

208
00:12:45,040 --> 00:12:50,360
instead of saying ‑and, I'm going to change that to be an ‑or because

209
00:12:50,360 --> 00:12:54,040
we know that it fails on this second one.

210
00:12:54,040 --> 00:13:01,340
So let me just add my else statement.

211
00:13:01,340 --> 00:13:02,010
Okay,

212
00:13:02,010 --> 00:13:05,840
there we go, and press Enter, and it should say Yes because it

213
00:13:05,840 --> 00:13:09,910
ignores the second one because it does a test and whichever one of

214
00:13:09,910 --> 00:13:12,420
those passes will allow it to move forward.

215
00:13:12,420 --> 00:13:15,570
So you need to be kind of a word of warning with that one is making

216
00:13:15,570 --> 00:13:17,540
sure that that's exactly what you want to do.

217
00:13:17,540 --> 00:13:20,520
Very rarely are you wanting to take two values and

218
00:13:20,520 --> 00:13:23,440
doing an or comparison between them.

219
00:13:23,440 --> 00:13:28,120
Okay, so our last one here that we want to look at quickly, if we just go down,

220
00:13:28,120 --> 00:13:32,480
is we want to look at the kind of numeric values and kind

221
00:13:32,480 --> 00:13:35,140
of how we can add those things together.

222
00:13:35,140 --> 00:13:36,010
Now it's really,

223
00:13:36,010 --> 00:13:39,210
really simple to kind of do. So what we can do here is

224
00:13:39,210 --> 00:13:41,760
we can kind of add things together.

225
00:13:41,760 --> 00:13:53,370
So if I say variable1 = 1 and variable2 = 5, I can then say, well,

226
00:13:53,370 --> 00:14:04,130
if $variable1 + $variable5, now notice I'm doing a sum here,

227
00:14:04,130 --> 00:14:09,020
so I need to kind of put those into a separate piece, then I can take

228
00:14:09,020 --> 00:14:13,490
the output of that and say if that is greater than 4,

229
00:14:13,490 --> 00:14:18,330
then do something. So you can see we, like normal math, you do

230
00:14:18,330 --> 00:14:21,220
the kind of calculations first and then you use the output of the

231
00:14:21,220 --> 00:14:22,770
calculations to do something else.

232
00:14:22,770 --> 00:14:25,400
So if variable1 + variable5,

233
00:14:25,400 --> 00:14:33,290
which should give us 6, is greater than 4, then Write‑Host "Yes",

234
00:14:33,290 --> 00:14:36,440
and we'll just just do a Yes output for now.

235
00:14:36,440 --> 00:14:38,360
So, then we have that, it will say, well,

236
00:14:38,360 --> 00:14:42,530
is it greater than it, is it less than it, and it adds those together. Now,

237
00:14:42,530 --> 00:14:46,440
of course, if we had a No option, then of course we could do that too.

238
00:14:46,440 --> 00:14:46,830
Now,

239
00:14:46,830 --> 00:14:48,720
what I want you to do is just look at the syntax for a

240
00:14:48,720 --> 00:14:51,930
second and wonder why it didn't execute.

241
00:14:51,930 --> 00:14:53,550
And when you look at it,

242
00:14:53,550 --> 00:14:57,780
you can kind of see that we've got an if statement where we've gone

243
00:14:57,780 --> 00:15:03,100
through and said if do this is this is greater than this and this, and

244
00:15:03,100 --> 00:15:06,270
should it return a Yes or should it return a No? Now,

245
00:15:06,270 --> 00:15:06,690
of course,

246
00:15:06,690 --> 00:15:14,080
depending on the calculation that you're doing, so we could say times, do

247
00:15:14,080 --> 00:15:19,840
that, and then you get that if variable1 * variable5, etc.

248
00:15:19,840 --> 00:15:22,510
Now you may wonder why it's still not working, hopefully you've all seen it,

249
00:15:22,510 --> 00:15:26,090
and it's because I named the variable the wrong name

250
00:15:26,090 --> 00:15:27,610
and it didn't return anything.

251
00:15:27,610 --> 00:15:30,200
So if I change that to variable2, then sure enough,

252
00:15:30,200 --> 00:15:32,560
I get the right name, I get the right value.

253
00:15:32,560 --> 00:15:36,220
If I change variable5 to variable2, I get the output as well.

254
00:15:36,220 --> 00:15:39,200
So just be aware. You'll notice it didn't error, it

255
00:15:39,200 --> 00:15:41,340
just kind of returned nothing.

256
00:15:41,340 --> 00:15:44,950
So it's critical that you name things correctly when you're

257
00:15:44,950 --> 00:15:47,970
building variables and that you make sure that you're using the

258
00:15:47,970 --> 00:15:50,480
right ones as you kind of go through your code.

259
00:15:50,480 --> 00:15:53,240
So that was a little bit of a test for you there.

260
00:15:53,240 --> 00:15:54,480
Okay, finally,

261
00:15:54,480 --> 00:15:58,060
the option that we have available to us now in PowerShell

262
00:15:58,060 --> 00:16:00,310
7 is obviously the ternary command.

263
00:16:00,310 --> 00:16:03,000
So let's do the same thing again.

264
00:16:03,000 --> 00:16:10,140
So this one is going to be a number 1 and $variable2 is going to be a number 3.

265
00:16:10,140 --> 00:16:14,450
Now, if we look at this syntax, it's a little bit easier.

266
00:16:14,450 --> 00:16:22,070
So variable1 is equal to variable2, which is the one that we use in.

267
00:16:22,070 --> 00:16:24,300
And then, of course, we simply do this one.

268
00:16:24,300 --> 00:16:27,400
So I'm going to just return a true value,

269
00:16:27,400 --> 00:16:31,230
and then I'm going to return a false value. So it should

270
00:16:31,230 --> 00:16:34,900
return false. Exactly the same as writing the if statements

271
00:16:34,900 --> 00:16:36,950
except it's a little bit cleaner,

272
00:16:36,950 --> 00:16:40,130
a little bit simpler, and easier to kind of return those values.

273
00:16:40,130 --> 00:16:50,000
So that was just some base examples of how to use the if and the else operators, as well as the ternary commands, etc., to help us define the flow of PowerShell.

