1
00:00:00,140 --> 00:00:04,940
So what are if and else statements within PowerShell?

2
00:00:04,940 --> 00:00:05,730
Well, firstly,

3
00:00:05,730 --> 00:00:10,940
an if and an else statement is used to control the flow

4
00:00:10,940 --> 00:00:13,310
of PowerShell execution for decisions.

5
00:00:13,310 --> 00:00:16,120
So, for example, when you run a PowerShell script,

6
00:00:16,120 --> 00:00:19,760
you have to make a decision and then perform different types of logic

7
00:00:19,760 --> 00:00:23,510
based on the outcome of that thing that you're evaluating.

8
00:00:23,510 --> 00:00:27,400
You may have a statement or value that needs to be checked,

9
00:00:27,400 --> 00:00:29,950
combined with something else or validated,

10
00:00:29,950 --> 00:00:36,040
and then execute different code based on different outputs of that evaluation.

11
00:00:36,040 --> 00:00:40,610
In PowerShell context or encoding, this is referred to as conditional execution,

12
00:00:40,610 --> 00:00:44,370
and this is where our if and else statements come from.

13
00:00:44,370 --> 00:00:47,080
Now when we talk about if statements,

14
00:00:47,080 --> 00:00:49,710
we can group them into three different types.

15
00:00:49,710 --> 00:00:54,020
The first one is the standard if statement.

16
00:00:54,020 --> 00:00:58,910
This is the regular one where there's a first test in the evaluation.

17
00:00:58,910 --> 00:01:02,240
So, for example, if this something,

18
00:01:02,240 --> 00:01:06,930
and then it will contain a statement of code execution after that.

19
00:01:06,930 --> 00:01:12,120
And then it kind of ends, so it's a singular if this or that doesn't,

20
00:01:12,120 --> 00:01:13,300
then do this.

21
00:01:13,300 --> 00:01:16,340
Else just drop it out and do nothing.

22
00:01:16,340 --> 00:01:19,710
The second one is to combine it with an else statement,

23
00:01:19,710 --> 00:01:23,120
which effectively has a default value at the end of it.

24
00:01:23,120 --> 00:01:26,150
So if the statement above doesn't pass,

25
00:01:26,150 --> 00:01:28,920
then just drop it into this default piece,

26
00:01:28,920 --> 00:01:35,240
where it will then execute whatever code or whatever output you've determined.

27
00:01:35,240 --> 00:01:38,150
And the last one is referred to as an elseif so,

28
00:01:38,150 --> 00:01:40,430
this is where we combine them all together.

29
00:01:40,430 --> 00:01:44,880
So we can now say if this meets, then do that.

30
00:01:44,880 --> 00:01:50,650
Elseif, which then provides additional conditions with different outcomes,

31
00:01:50,650 --> 00:01:54,840
then do this, elseif do this, else, which is the default,

32
00:01:54,840 --> 00:01:56,140
drop it out.

33
00:01:56,140 --> 00:01:59,050
So this allows us to build quite deep structures.

34
00:01:59,050 --> 00:01:59,480
Now, of course,

35
00:01:59,480 --> 00:02:02,570
don't go crazy and build loads of if and elseif

36
00:02:02,570 --> 00:02:04,490
statements and there's hundreds of them.

37
00:02:04,490 --> 00:02:07,150
Because trust me, your code's going to take ages to execute.

38
00:02:07,150 --> 00:02:10,870
But it does provide us with the ability to create quite

39
00:02:10,870 --> 00:02:16,540
complex process and paths for execution.

40
00:02:16,540 --> 00:02:18,780
Now, when we talk about these three different types,

41
00:02:18,780 --> 00:02:21,260
the if, the elseif, and the else statements,

42
00:02:21,260 --> 00:02:24,820
they're kind of made up in a logical order of components.

43
00:02:24,820 --> 00:02:27,490
The first piece is the if statement.

44
00:02:27,490 --> 00:02:30,610
This is the if something is going to happen,

45
00:02:30,610 --> 00:02:33,320
and this is wrapped around with parentheses.

46
00:02:33,320 --> 00:02:36,640
So if we're looking at it in code, it would say if,

47
00:02:36,640 --> 00:02:40,180
open bracket, something in the middle, close bracket.

48
00:02:40,180 --> 00:02:43,000
And the thing that's inside the brackets or the

49
00:02:43,000 --> 00:02:45,600
parentheses is the bit that's evaluated.

50
00:02:45,600 --> 00:02:50,510
Then we can add any elseif statements to add multiple conditions.

51
00:02:50,510 --> 00:02:54,760
Then we have the else statement, which is the dropout,

52
00:02:54,760 --> 00:02:56,800
or the final default value.

53
00:02:56,800 --> 00:03:00,240
And then, of course, we have the actual output and value,

54
00:03:00,240 --> 00:03:02,670
because we may be writing a value to the screen,

55
00:03:02,670 --> 00:03:07,230
or we may be calculating something and then using that output later on.

56
00:03:07,230 --> 00:03:10,660
So these statements allow us to use either or.

57
00:03:10,660 --> 00:03:12,950
Now when we look at this a little bit further,

58
00:03:12,950 --> 00:03:15,790
the if statement, when it executes,

59
00:03:15,790 --> 00:03:20,600
it checks the result of the first test to make sure that it equals to true.

60
00:03:20,600 --> 00:03:22,780
And then if it does and passes,

61
00:03:22,780 --> 00:03:26,770
it will execute any kind of actions or code that

62
00:03:26,770 --> 00:03:30,140
reside in that initial if statement.

63
00:03:30,140 --> 00:03:32,980
If the first test result returns false,

64
00:03:32,980 --> 00:03:36,600
then it will work down the tree for every other

65
00:03:36,600 --> 00:03:39,880
condition to evaluate until they are true,

66
00:03:39,880 --> 00:03:45,440
or if they all equal false then it just drops out to the bottom.

67
00:03:45,440 --> 00:03:49,280
If the result of all the subsequent tests equals true,

68
00:03:49,280 --> 00:03:53,150
then any specified actions or code is executed.

69
00:03:53,150 --> 00:03:56,480
So if we have if‑else, if‑else, if‑else,

70
00:03:56,480 --> 00:03:58,530
it could drop to the second or the first one,

71
00:03:58,530 --> 00:04:00,360
depending what was passed.

72
00:04:00,360 --> 00:04:04,340
And then, of course, as we know, if all of them fail,

73
00:04:04,340 --> 00:04:08,420
it will literally just drop to that, what I refer to as the default ending,

74
00:04:08,420 --> 00:04:13,540
or the else, which basically will say just execute this piece of code.

75
00:04:13,540 --> 00:04:15,690
So what does that actually look like?

76
00:04:15,690 --> 00:04:19,420
Well, if you look at the syntax here, I'm going to have a variable with a value.

77
00:04:19,420 --> 00:04:23,300
It's just a variable called variable, and then I have an if statement.

78
00:04:23,300 --> 00:04:25,610
This is the simplistic syntax.

79
00:04:25,610 --> 00:04:27,870
It's if, and I'm using a variable.

80
00:04:27,870 --> 00:04:31,740
Now it could just be a piece of text I write there or something else, but

81
00:04:31,740 --> 00:04:35,840
predominantly you'll be validating the input of something.

82
00:04:35,840 --> 00:04:38,730
So if my variable has a value,

83
00:04:38,730 --> 00:04:42,930
I'm not validating what the value is; I'm just using it this way to say,

84
00:04:42,930 --> 00:04:45,040
does it contain something?

85
00:04:45,040 --> 00:04:47,660
If it does, then write that output,

86
00:04:47,660 --> 00:04:51,080
and you can see I'm just passing that same value to the output.

87
00:04:51,080 --> 00:04:54,400
So in this example, it's literally just one test and drop out.

88
00:04:54,400 --> 00:04:56,240
Notice there's no else.

89
00:04:56,240 --> 00:04:57,180
There's no elseif.

90
00:04:57,180 --> 00:05:00,940
It's literally just an if statement.

91
00:05:00,940 --> 00:05:05,190
If we were to enhance this and go with an if and an else syntax,

92
00:05:05,190 --> 00:05:08,840
then of course I have my variable, which is equal to the value,

93
00:05:08,840 --> 00:05:11,220
I have my first if, which we just looked at,

94
00:05:11,220 --> 00:05:15,640
so if the variable contains something, then pass this,

95
00:05:15,640 --> 00:05:19,340
else drop it out into the bottom.

96
00:05:19,340 --> 00:05:22,380
So now what we've done is added a default path,

97
00:05:22,380 --> 00:05:25,730
that if the first one fails, it will output the value.

98
00:05:25,730 --> 00:05:29,540
And, of course, I've changed the message to say the check returned false.

99
00:05:29,540 --> 00:05:30,980
So let's go one step further.

100
00:05:30,980 --> 00:05:35,310
Let's add an elseif and an else. So now I have two variables,

101
00:05:35,310 --> 00:05:38,340
variableone, variabletwo, both with values,

102
00:05:38,340 --> 00:05:41,620
and I'm going to say if variableone contains a value,

103
00:05:41,620 --> 00:05:45,160
then write the output, and that is the end of the code.

104
00:05:45,160 --> 00:05:49,140
Elseif, so if variableone fails,

105
00:05:49,140 --> 00:05:53,450
drop down to elseif and check if variabletwo has a value,

106
00:05:53,450 --> 00:05:56,670
and if it does, then execute that code path.

107
00:05:56,670 --> 00:05:57,600
Otherwise,

108
00:05:57,600 --> 00:06:00,820
just drop it out into the else statement and write the message

109
00:06:00,820 --> 00:06:03,140
that both the values don't have anything.

110
00:06:03,140 --> 00:06:05,840
So you can see the structure is really, really simple,

111
00:06:05,840 --> 00:06:10,240
but you can also probably get a sense of how complicated it could be,

112
00:06:10,240 --> 00:06:15,540
depending on how many statements you need to kind of flow through.

113
00:06:15,540 --> 00:06:17,550
So let's talk about the parentheses themselves.

114
00:06:17,550 --> 00:06:20,080
So in the if statement so far, we've just used a variable,

115
00:06:20,080 --> 00:06:23,450
so if this object contains something.

116
00:06:23,450 --> 00:06:24,160
However,

117
00:06:24,160 --> 00:06:29,840
the parentheses contain the evaluation of a condition or multiple conditions.

118
00:06:29,840 --> 00:06:34,880
It can contain comparison operators, so some examples would be eq for equal,

119
00:06:34,880 --> 00:06:40,290
gt for greater than, ne for not equal, like for like,

120
00:06:40,290 --> 00:06:42,940
notlike, matches, etc.

121
00:06:42,940 --> 00:06:47,320
We can also do a comparison of array values as well.

122
00:06:47,320 --> 00:06:51,870
So if we have a‑‑‑imagine a set of numbers in a variable,

123
00:06:51,870 --> 00:06:54,470
so numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

124
00:06:54,470 --> 00:06:58,940
we could check if a number existed in that array.

125
00:06:58,940 --> 00:07:02,000
We could also do combinations of and and or.

126
00:07:02,000 --> 00:07:04,840
So if we're trying to do multiple comparison and say,

127
00:07:04,840 --> 00:07:08,190
well, does this value and this value or this value,

128
00:07:08,190 --> 00:07:09,840
then we can do that too.

129
00:07:09,840 --> 00:07:13,810
And then there's also support for multiple conditions if we need to.

130
00:07:13,810 --> 00:07:16,550
So this helps us to do some very,

131
00:07:16,550 --> 00:07:20,400
very deep and complex if comparisons and statements based

132
00:07:20,400 --> 00:07:23,940
on multiple values that we may pass in.

133
00:07:23,940 --> 00:07:27,840
So what are some of the common condition operators that are available?

134
00:07:27,840 --> 00:07:31,130
Well, the first one that we kind of use the most is comparison,

135
00:07:31,130 --> 00:07:36,840
so a comparison operator is used to compare values and test conditions.

136
00:07:36,840 --> 00:07:40,010
The comparison operators include operators that can

137
00:07:40,010 --> 00:07:43,190
find or replace patterns in text, so, for example,

138
00:07:43,190 --> 00:07:46,380
using match, notmatch, replace, etc.

139
00:07:46,380 --> 00:07:52,240
Or we can use regular expressions, such as like, notlike, or even wildcards.

140
00:07:52,240 --> 00:07:55,780
You can also then do what's called containment comparison,

141
00:07:55,780 --> 00:07:59,490
which means that we're trying to look within something to see if

142
00:07:59,490 --> 00:08:02,590
the value that I have exists in something,

143
00:08:02,590 --> 00:08:07,470
and this uses such as in, notin, contains,

144
00:08:07,470 --> 00:08:09,640
and notcontains.

145
00:08:09,640 --> 00:08:13,220
The second operator that we can use, or common condition operator,

146
00:08:13,220 --> 00:08:15,240
is collection operators.

147
00:08:15,240 --> 00:08:19,330
This is where we can look for values that exist in an array of

148
00:08:19,330 --> 00:08:22,470
a data structure or a collection of items.

149
00:08:22,470 --> 00:08:26,000
The items could be of the same type, or they could be of different types.

150
00:08:26,000 --> 00:08:31,940
So your simple example here would be a folder on a file share with documents in.

151
00:08:31,940 --> 00:08:34,630
So that would be a collection that we would retrieve,

152
00:08:34,630 --> 00:08:36,740
and then we could iterate through them.

153
00:08:36,740 --> 00:08:40,090
We also then have logical operators, which are some of the most common.

154
00:08:40,090 --> 00:08:42,530
These are the logical operators such as and,

155
00:08:42,530 --> 00:08:48,400
or, not, and this is to connect conditional statements into complex conditions.

156
00:08:48,400 --> 00:08:49,340
So, for example,

157
00:08:49,340 --> 00:08:54,100
you could use a logical and operator to then create a filter

158
00:08:54,100 --> 00:08:57,840
with one or more different conditions.

159
00:08:57,840 --> 00:09:02,350
We also have arithmetic operators, where this can calculate numeric values.

160
00:09:02,350 --> 00:09:06,010
You can use one or more arithmetic operators to add,

161
00:09:06,010 --> 00:09:08,670
subtract, multiply, and divide values,

162
00:09:08,670 --> 00:09:12,740
as well as to calculate any kind of sum based on the

163
00:09:12,740 --> 00:09:15,340
various numbers that you might be using.

164
00:09:15,340 --> 00:09:18,760
Now from the conditions operators,

165
00:09:18,760 --> 00:09:22,290
the first one that tends to get used the most is assignment.

166
00:09:22,290 --> 00:09:28,760
So assignment operators are =, +=, ‑=, etc.

167
00:09:28,760 --> 00:09:33,450
This is used to assign, change, or append values to a variable.

168
00:09:33,450 --> 00:09:36,740
So for example, if we were iterating through numbers,

169
00:09:36,740 --> 00:09:41,350
and we wanted to increase or add a value to a variable,

170
00:09:41,350 --> 00:09:45,570
we can simply use += and pass the value into the variable,

171
00:09:45,570 --> 00:09:47,740
and that will append to that.

172
00:09:47,740 --> 00:09:50,430
We can also use what's called redirection operators.

173
00:09:50,430 --> 00:09:53,030
These would be >, >>,

174
00:09:53,030 --> 00:09:57,550
etc., and this is something we could do in batch files years ago where

175
00:09:57,550 --> 00:10:01,540
we can basically send the output to a text file.

176
00:10:01,540 --> 00:10:06,670
We can also use split and join, so this is called ‑split and ‑join operators,

177
00:10:06,670 --> 00:10:10,640
where we're able to divide and combine string values

178
00:10:10,640 --> 00:10:14,040
and even create substrings if needed.

179
00:10:14,040 --> 00:10:18,150
We also have the type object, which is, we can use the is,

180
00:10:18,150 --> 00:10:19,460
isnot, and as,

181
00:10:19,460 --> 00:10:24,780
and this is all about casting and checking the .NET Framework type of the

182
00:10:24,780 --> 00:10:28,940
value or the object that we retrieved in PowerShell.

183
00:10:28,940 --> 00:10:31,050
We also have what's called unary operators,

184
00:10:31,050 --> 00:10:33,860
which is all about incrementing or decrementing variables.

185
00:10:33,860 --> 00:10:35,710
You may have seen PowerShell in the past,

186
00:10:35,710 --> 00:10:37,820
where we start with a number, for example,

187
00:10:37,820 --> 00:10:43,850
so like a = 1, and then we want to increment and you'll often see $a++.

188
00:10:43,850 --> 00:10:50,640
That's a unary operator that will basically add another 1 to what's there.

189
00:10:50,640 --> 00:10:54,570
And then, of course, our last one here is what's referred to as bitwise.

190
00:10:54,570 --> 00:10:59,990
Now bitwise operators are band, bor, bxor,

191
00:10:59,990 --> 00:11:04,740
bnot, etc., and this manipulates the bit patterns in the values,

192
00:11:04,740 --> 00:11:09,000
so a bitwise operators act at the binary format of a value.

193
00:11:09,000 --> 00:11:11,840
So, for example, if you had the number 10,

194
00:11:11,840 --> 00:11:16,670
the binary value for that is 00001010.

195
00:11:16,670 --> 00:11:20,200
And if you were then trying to add that or compare it to number 3,

196
00:11:20,200 --> 00:11:28,740
its binary value is 00000011, so it uses that level.

197
00:11:28,740 --> 00:11:31,440
Now the most common use of the if statement is

198
00:11:31,440 --> 00:11:34,540
comparing two items with each other.

199
00:11:34,540 --> 00:11:38,520
So PowerShell has multiple operators for the comparison scenarios,

200
00:11:38,520 --> 00:11:40,720
and when you use a comparison operator,

201
00:11:40,720 --> 00:11:46,140
you compare the left‑hand side's value to the right‑hand side value.

202
00:11:46,140 --> 00:11:49,510
Now from a syntax perspective, let's say we have two variables.

203
00:11:49,510 --> 00:11:55,040
Variable = value, and then we have a comparable variable.

204
00:11:55,040 --> 00:11:59,560
If I want to check if these are equal to each other in an if statement,

205
00:11:59,560 --> 00:12:02,690
I can say if, parentheses, variable,

206
00:12:02,690 --> 00:12:06,110
and then we're going to use the ‑eq operator to validate.

207
00:12:06,110 --> 00:12:09,740
So that test, both those would need to be exactly the same.

208
00:12:09,740 --> 00:12:14,640
And of course, my value for both of those is value, so that would be successful.

209
00:12:14,640 --> 00:12:16,580
I could then also change that and say, well,

210
00:12:16,580 --> 00:12:21,900
is variable ‑gt comparevariable? Now what's going to happen here is

211
00:12:21,900 --> 00:12:24,840
this is probably going to fail because value and value are text

212
00:12:24,840 --> 00:12:26,700
values and they are equal to each other,

213
00:12:26,700 --> 00:12:28,860
so one cannot be greater than the other.

214
00:12:28,860 --> 00:12:31,840
This is normally used when we're using numbers.

215
00:12:31,840 --> 00:12:32,310
Then of course,

216
00:12:32,310 --> 00:12:36,080
we've got like, which will do a comparison check and say is

217
00:12:36,080 --> 00:12:39,590
variable like the comparevariable? If it is,

218
00:12:39,590 --> 00:12:42,720
then that will pass. And then we can do a match.

219
00:12:42,720 --> 00:12:44,660
This is really used for string values.

220
00:12:44,660 --> 00:12:47,640
So does this value match exactly that value?

221
00:12:47,640 --> 00:12:53,340
So a couple of different operators that we can use for doing comparisons. Now a

222
00:12:53,340 --> 00:12:58,640
comparison operator kind of returns either true or false.

223
00:12:58,640 --> 00:13:00,650
Collection operators, on the other hand,

224
00:13:00,650 --> 00:13:05,090
differ a little bit. As each item in the collection is evaluated, the

225
00:13:05,090 --> 00:13:09,640
operator will then return whether that value is true or not.

226
00:13:09,640 --> 00:13:13,720
So from a syntax perspective, let's say we have two variables here,

227
00:13:13,720 --> 00:13:15,670
so I have an array.

228
00:13:15,670 --> 00:13:17,340
You'll notice we're doing something new.

229
00:13:17,340 --> 00:13:19,590
It says 1 . . 10.

230
00:13:19,590 --> 00:13:26,010
What that does is that creates an array object of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

231
00:13:26,010 --> 00:13:28,640
And then I have my comparevariable.

232
00:13:28,640 --> 00:13:32,700
So what I can do here is I can say if the current array value, so

233
00:13:32,700 --> 00:13:37,030
array, something, is greater than comparevariable, then,

234
00:13:37,030 --> 00:13:39,200
of course, it will return true.

235
00:13:39,200 --> 00:13:42,700
So that's determining that my array variable is one

236
00:13:42,700 --> 00:13:45,340
of the numbers between 1 and 10.

237
00:13:45,340 --> 00:13:48,790
I can then say, well, if the array contains that number,

238
00:13:48,790 --> 00:13:50,720
so slightly different approach,

239
00:13:50,720 --> 00:13:52,860
we're actually trying to see if it contains the number

240
00:13:52,860 --> 00:13:55,030
we're trying to compare. And then, of course,

241
00:13:55,030 --> 00:13:58,560
we can flick it around the other way and say the

242
00:13:58,560 --> 00:14:03,360
comparevariable, is it in the array? So a different way of

243
00:14:03,360 --> 00:14:07,140
kind of comparing if the values exist.

244
00:14:07,140 --> 00:14:11,550
Now logical operators are used to invert or combine other expressions.

245
00:14:11,550 --> 00:14:15,000
You can connect multiple statements allowing you to use a single

246
00:14:15,000 --> 00:14:18,440
expression to test for numerous conditions.

247
00:14:18,440 --> 00:14:20,140
So what does that look like?

248
00:14:20,140 --> 00:14:22,600
So let's say we have three variables now.

249
00:14:22,600 --> 00:14:24,340
I have my base variable,

250
00:14:24,340 --> 00:14:29,380
my comparevariable, and another comparevariable. In this instance,

251
00:14:29,380 --> 00:14:31,320
when we're using logical operators,

252
00:14:31,320 --> 00:14:34,570
you can see that we've got two equals statements

253
00:14:34,570 --> 00:14:37,310
which are then combined into an ‑and,

254
00:14:37,310 --> 00:14:41,150
which means that my code needs to pass both of these.

255
00:14:41,150 --> 00:14:46,530
So is variableOne ‑eq the comparevariableOne? And ‑and, is

256
00:14:46,530 --> 00:14:50,330
$variable ‑eq comparevariableTwo? Now, of course,

257
00:14:50,330 --> 00:14:55,140
we can change that to an ‑or, which means it takes one or the other.

258
00:14:55,140 --> 00:14:59,800
So either of these approaches will work, except the output or the outcome may

259
00:14:59,800 --> 00:15:05,240
be different, because ‑and requires both; ‑or can be either.

260
00:15:05,240 --> 00:15:08,620
Arithmetic operators obviously can calculate numeric values,

261
00:15:08,620 --> 00:15:12,050
which is critical in lots of PowerShell that you may write, where you'll

262
00:15:12,050 --> 00:15:15,840
need to pass values and calculate and multiply, etc.

263
00:15:15,840 --> 00:15:18,400
Now, of course, when we do this in PowerShell,

264
00:15:18,400 --> 00:15:19,890
if we look at the syntax here,

265
00:15:19,890 --> 00:15:23,580
I've got my three variables again with just text values.

266
00:15:23,580 --> 00:15:26,240
Obviously, in the real world, they would be actual numbers.

267
00:15:26,240 --> 00:15:30,000
And of course, what you can see here is I'm doing a combination of things.

268
00:15:30,000 --> 00:15:34,470
I'm using an arithmetic operator, which would be +, so I can say,

269
00:15:34,470 --> 00:15:39,040
if my first variable + the second variable ‑gt the third variable,

270
00:15:39,040 --> 00:15:40,440
then do something.

271
00:15:40,440 --> 00:15:44,020
I could change that to division and then use less than. I could

272
00:15:44,020 --> 00:15:46,870
also say multiply those together and then,

273
00:15:46,870 --> 00:15:49,210
of course, equal to that variable.

274
00:15:49,210 --> 00:15:52,340
Now of course, you can't do that with string values.

275
00:15:52,340 --> 00:15:53,360
It will complain.

276
00:15:53,360 --> 00:15:54,830
It doesn't know how to add those.

277
00:15:54,830 --> 00:16:00,140
So when we actually write the PowerShell out we'll utilize numbers instead.

278
00:16:00,140 --> 00:16:03,400
Now another operator that's available to us is what's called the turnary

279
00:16:03,400 --> 00:16:09,220
operator. Now PowerShell 7 upwards introduced a new syntax for using if and

280
00:16:09,220 --> 00:16:13,620
else instead of writing if and else. So first off,

281
00:16:13,620 --> 00:16:15,930
we have the condition, the same as you would normally.

282
00:16:15,930 --> 00:16:18,600
So the condition expression gets evaluated and the

283
00:16:18,600 --> 00:16:22,840
result returns a Boolean value.

284
00:16:22,840 --> 00:16:26,890
Then we have a true expression, so if it passes that true,

285
00:16:26,890 --> 00:16:29,360
then it will execute the specific code path.

286
00:16:29,360 --> 00:16:34,060
And then, of course, we have a false execution as well, so if it fails the true,

287
00:16:34,060 --> 00:16:38,940
then it will execute this, so very much the same as an if‑else statement.

288
00:16:38,940 --> 00:16:41,640
This syntax, however, is very different.

289
00:16:41,640 --> 00:16:43,640
So let's take a look at our three variables.

290
00:16:43,640 --> 00:16:46,970
I have variableOne with a number, variableTwo with a number,

291
00:16:46,970 --> 00:16:50,740
and then a text variable with the word true.

292
00:16:50,740 --> 00:16:53,450
Now what we can see is, I have a couple of different options here.

293
00:16:53,450 --> 00:16:56,660
So the first one, notice I'm just using parentheses,

294
00:16:56,660 --> 00:16:57,480
and I say,

295
00:16:57,480 --> 00:17:02,280
if value 1 is equal to value 2 and then if we ignore

296
00:17:02,280 --> 00:17:06,660
the question mark for a second, it says $true : $false.

297
00:17:06,660 --> 00:17:08,470
Now think of our logic.

298
00:17:08,470 --> 00:17:12,150
Imagine you're writing an if‑else. The if would be before the

299
00:17:12,150 --> 00:17:15,840
parentheses, so if variableOne ‑eq variableTwo,

300
00:17:15,840 --> 00:17:19,540
and then the question mark is the true path.

301
00:17:19,540 --> 00:17:21,950
So the question mark says, well, if it's true,

302
00:17:21,950 --> 00:17:25,040
do this, and the colon is the false.

303
00:17:25,040 --> 00:17:27,540
So the second one here, for example, I've gone a little bit further.

304
00:17:27,540 --> 00:17:32,640
It says variableOne ‑eq availableTwo. If it's true,

305
00:17:32,640 --> 00:17:36,340
write out some text values that say variableOne >

306
00:17:36,340 --> 00:17:41,240
variableTwo. Else, variableOne < variableTwo.

307
00:17:41,240 --> 00:17:43,890
Now, of course, we can also do this with a text value as well.

308
00:17:43,890 --> 00:17:47,620
So we can say, you know, remember we put in parentheses,

309
00:17:47,620 --> 00:17:50,100
text variable or the variable name that just says if

310
00:17:50,100 --> 00:17:52,830
something exists? So in this one,

311
00:17:52,830 --> 00:17:57,490
I'm just saying is there a value? which is true. It will

312
00:17:57,490 --> 00:17:59,170
then write the message, Value is true.

313
00:17:59,170 --> 00:18:01,140
Else, the value is false.

314
00:18:01,140 --> 00:18:03,930
This works great for inline PowerShell as you're writing

315
00:18:03,930 --> 00:18:07,300
things out. It's also significantly less than writing lots of

316
00:18:07,300 --> 00:18:13,000
isif and else and elseif statements. And so this works out really, really well.

