1
00:00:01,040 --> 00:00:03,460
Next up is the ternary operator.

2
00:00:03,460 --> 00:00:07,840
This is also something that was introduced in Powershell 7.

3
00:00:07,840 --> 00:00:13,660
This is an alternative to using an if else statement in PowerShell. This

4
00:00:13,660 --> 00:00:17,670
operator is also described in the about_if help topic,

5
00:00:17,670 --> 00:00:20,840
which again, I'm always encouraging you to read the help.

6
00:00:20,840 --> 00:00:24,240
So the way this operator works is like this.

7
00:00:24,240 --> 00:00:26,940
We have some condition to test. That test has to

8
00:00:26,940 --> 00:00:29,740
return a value of true or false.

9
00:00:29,740 --> 00:00:31,570
If the value is true,

10
00:00:31,570 --> 00:00:34,770
then we run whatever code is on the right side of

11
00:00:34,770 --> 00:00:37,520
the question mark. If it's false,

12
00:00:37,520 --> 00:00:43,440
then we run whatever code is on the right side of the colon.

13
00:00:43,440 --> 00:00:48,160
Traditionally, and this is what we would do in Windows PowerShell 5.1,

14
00:00:48,160 --> 00:00:52,350
if 2 is greater than 1, then display yes, else,

15
00:00:52,350 --> 00:00:53,940
display no.

16
00:00:53,940 --> 00:00:58,250
And so we can see yes as the result. And here is the same

17
00:00:58,250 --> 00:01:01,920
expression rewritten using the ternary operator, 2 greater

18
00:01:01,920 --> 00:01:08,270
equal to 1. If that's true, then yes, otherwise, : no,

19
00:01:08,270 --> 00:01:12,350
and you can see the result of yes. So here is a little more complex

20
00:01:12,350 --> 00:01:15,620
example. I'm going to do Get‑Process notepad. Hey,

21
00:01:15,620 --> 00:01:16,820
is Notepad running?

22
00:01:16,820 --> 00:01:21,290
If it is, then I want to stop Notepad, otherwise, I'm going to

23
00:01:21,290 --> 00:01:24,950
write a warning saying Notepad is not running. When you're using

24
00:01:24,950 --> 00:01:27,620
the ternary operator in this regard,

25
00:01:27,620 --> 00:01:31,610
put the expression that you want to evaluate and run in

26
00:01:31,610 --> 00:01:37,640
parentheses. If Notepad is not running, which it isn't in this example here,

27
00:01:37,640 --> 00:01:41,490
then I get my Get‑Process warning and then I get the code that

28
00:01:41,490 --> 00:01:45,040
executes after the colon, in this case, the warning which says

29
00:01:45,040 --> 00:01:49,450
Notepad is not running. Using the ternary operator is great for

30
00:01:49,450 --> 00:01:51,850
really simple if else logic,

31
00:01:51,850 --> 00:01:54,640
especially if you want to do something like this at the

32
00:01:54,640 --> 00:01:56,900
console. When you get to scripting,

33
00:01:56,900 --> 00:02:01,590
my belief, it's just as easy to use an if else statement because

34
00:02:01,590 --> 00:02:05,310
the ternary operator does require PowerShell 7 so you'd have to

35
00:02:05,310 --> 00:02:09,310
take that into account in your scripts, but I don't want to get

36
00:02:09,310 --> 00:02:11,840
too far ahead of us.

37
00:02:11,840 --> 00:02:13,340
Let me show you a few other things here about the

38
00:02:13,340 --> 00:02:17,140
ternary operator. Here is another example,

39
00:02:17,140 --> 00:02:21,510
remember the expression that's on the left side of the question mark has to

40
00:02:21,510 --> 00:02:26,300
be either true or false. $isWindows is another one of these built‑in

41
00:02:26,300 --> 00:02:29,660
variables that is a Boolean value of true or false.

42
00:02:29,660 --> 00:02:36,240
If I'm running PowerShell 7 on Windows, that will have a result of true.

43
00:02:36,240 --> 00:02:40,140
If I'm running this on Linux or Mac, that result will be false.

44
00:02:40,140 --> 00:02:44,040
So if I'm running this on Windows then I can run

45
00:02:44,040 --> 00:02:48,440
Get‑CimInstance, and I'm using the alias gcim, and say hey

46
00:02:48,440 --> 00:02:52,740
get me the win32_operatingsystem w my class.

47
00:02:52,740 --> 00:02:56,850
Otherwise, if I'm not running on Windows, then run a

48
00:02:56,850 --> 00:02:59,990
Linux command, the lsb_release. Now,

49
00:02:59,990 --> 00:03:02,330
I don't know for a fact that will run on all Linux

50
00:03:02,330 --> 00:03:04,640
installs or all non‑Window systems,

51
00:03:04,640 --> 00:03:08,510
but I just want you to kind of get an idea of if I'm running Windows,

52
00:03:08,510 --> 00:03:15,000
then I want to run a Windows‑specific command, otherwise I'm going to run a non‑Windows command.

