1
00:00:01,140 --> 00:00:05,170
Next, let's take a look at some new PowerShell 7 operators that were

2
00:00:05,170 --> 00:00:09,140
designed to make it easier to work with null values.

3
00:00:09,140 --> 00:00:11,790
These will make your head hurt a little bit, and they will

4
00:00:11,790 --> 00:00:14,440
take some time to get your head around.

5
00:00:14,440 --> 00:00:18,220
And I encourage you, again, to try the demos that I have in the course

6
00:00:18,220 --> 00:00:21,340
downloads so you can see how these things work for yourself.

7
00:00:21,340 --> 00:00:25,440
But at least let me give you a quick introduction to these operators.

8
00:00:25,440 --> 00:00:27,880
So I'm going to create a variable, $n, and basically

9
00:00:27,880 --> 00:00:32,080
assign a null value to it using $null,

10
00:00:32,080 --> 00:00:35,780
a special built‑in variable in PowerShell. The

11
00:00:35,780 --> 00:00:40,440
null‑coalescing operator is the two question marks.

12
00:00:40,440 --> 00:00:44,330
So if the left side of that operator is null,

13
00:00:44,330 --> 00:00:49,040
then evaluate or run the code that's on the right side.

14
00:00:49,040 --> 00:00:54,670
So if $n is null, then use the Write‑Warning command and say,

15
00:00:54,670 --> 00:00:58,100
hey, show me that the variable is null,

16
00:00:58,100 --> 00:01:01,340
which is exactly the result that I am expecting.

17
00:01:01,340 --> 00:01:02,840
Here's another example.

18
00:01:02,840 --> 00:01:06,770
So I'm going to assign the result of the expression

19
00:01:06,770 --> 00:01:09,530
$PSEdition ??, null‑coalescing,

20
00:01:09,530 --> 00:01:13,960
I'm not quite sure what a good way to describe the the two

21
00:01:13,960 --> 00:01:18,040
question marks is yet. If the left side is not null,

22
00:01:18,040 --> 00:01:19,810
then the right side is never used.

23
00:01:19,810 --> 00:01:26,340
So, $PSEdition should exist on all computers. And so if it does exist,

24
00:01:26,340 --> 00:01:31,840
PowerShell will assign that value to $v. Otherwise, if for some

25
00:01:31,840 --> 00:01:36,300
reason it doesn't exist, then unknown will be assigned to the value

26
00:01:36,300 --> 00:01:40,210
of $v. And because $PSEdition exists,

27
00:01:40,210 --> 00:01:44,010
then $v has a value, in this case, of Core because I'm

28
00:01:44,010 --> 00:01:46,940
running this on a PowerShell 7 system.

29
00:01:46,940 --> 00:01:51,570
So this null‑coalescing operator becomes a handy shortcut way

30
00:01:51,570 --> 00:01:54,930
comparing values to see if it's null or not and then deciding

31
00:01:54,930 --> 00:01:57,410
what else to do, very quick, simple,

32
00:01:57,410 --> 00:02:02,950
handy way. Related to that is the null‑coalescing assignment operator.

33
00:02:02,950 --> 00:02:06,840
That's the two question marks and the equal sign.

34
00:02:06,840 --> 00:02:12,040
So in this situation, we're going to assign the value from the right‑hand

35
00:02:12,040 --> 00:02:15,840
side to the left‑hand side if the left‑hand side is null.

36
00:02:15,840 --> 00:02:20,620
So $n is assigned the value of null. If $n is null,

37
00:02:20,620 --> 00:02:23,840
then we're going to give it a value of foo.

38
00:02:23,840 --> 00:02:27,280
So now, if I were to look at $n, I would see that it has a

39
00:02:27,280 --> 00:02:30,940
value of foo, and sure enough, it does.

40
00:02:30,940 --> 00:02:35,660
So let's rerun that command, $n, if it's null, then assign a value

41
00:02:35,660 --> 00:02:40,600
of bar. What do you think the value of $n is now, considering what I

42
00:02:40,600 --> 00:02:46,280
just did a moment ago? If you guessed foo, you are correct. Because

43
00:02:46,280 --> 00:02:49,940
$n was null to begin with,

44
00:02:49,940 --> 00:02:56,110
I assigned it a value of foo because $n was null, $n is no longer null,

45
00:02:56,110 --> 00:02:59,840
so the bar command never runs.

46
00:02:59,840 --> 00:03:03,450
So let's look at this from a more practical perspective. I'm going to create

47
00:03:03,450 --> 00:03:09,070
a variable, $p, and I want that value of p to be all the processes that have

48
00:03:09,070 --> 00:03:12,940
a working set size of greater than 1 gigabyte.

49
00:03:12,940 --> 00:03:18,440
If $p is null, if that Get‑Process command doesn't return anything,

50
00:03:18,440 --> 00:03:23,540
then $p will have null, in which case, I want to assign a value of 0.

51
00:03:23,540 --> 00:03:28,270
So because the Get‑Process command resulted in no processes, the

52
00:03:28,270 --> 00:03:33,540
null‑coalescing assignment command ran successfully, assigned a value of

53
00:03:33,540 --> 00:03:40,440
0 to $p, and you can see that $p has a value of 0.

54
00:03:40,440 --> 00:03:46,330
The last null‑related operator I want to talk about is the question mark period.

55
00:03:46,330 --> 00:03:50,360
And this is an operator that we use when we're trying to access members

56
00:03:50,360 --> 00:03:54,140
of an object, and specifically, methods of an object.

57
00:03:54,140 --> 00:03:57,610
So I'm going to, just to show you first how things normally work, and

58
00:03:57,610 --> 00:04:02,040
then we'll show how it would work when things are null.

59
00:04:02,040 --> 00:04:05,570
So I run Get‑Process for an existing process such as the current

60
00:04:05,570 --> 00:04:10,530
PowerShell process and save that to a variable $p, and now I want

61
00:04:10,530 --> 00:04:12,940
to invoke a method of an object.

62
00:04:12,940 --> 00:04:18,040
All objects have a ToString method, so this is a safe thing to test with.

63
00:04:18,040 --> 00:04:20,010
So if I do $p.ToString(),

64
00:04:20,010 --> 00:04:22,350
and remember when you invoke methods, you need to include the

65
00:04:22,350 --> 00:04:25,600
parentheses, I get the result that I expect.

66
00:04:25,600 --> 00:04:29,240
And this works because $p is not null.

67
00:04:29,240 --> 00:04:33,540
But, what if $p was null? Now in my demo here, I'm just

68
00:04:33,540 --> 00:04:37,340
going to assign a value of null to $p.

69
00:04:37,340 --> 00:04:41,670
But maybe in the real world, you forgot to create $p, or maybe

70
00:04:41,670 --> 00:04:46,700
you're using $p that was defined by some external process that

71
00:04:46,700 --> 00:04:48,810
failed to create it, and you have no idea.

72
00:04:48,810 --> 00:04:53,110
And this happens a lot in PowerShell. We work with things that maybe I got a

73
00:04:53,110 --> 00:04:57,130
result, maybe I didn't, let's see. And again, these are things that you'll

74
00:04:57,130 --> 00:04:59,540
probably work with more when you get to scripting,

75
00:04:59,540 --> 00:05:02,840
but we can work with this in the console as well.

76
00:05:02,840 --> 00:05:06,430
So I have $p. And in this particular case, it's

77
00:05:06,430 --> 00:05:08,940
null, but maybe I don't know that.

78
00:05:08,940 --> 00:05:12,440
Now if I try to invoke that same method,

79
00:05:12,440 --> 00:05:16,650
I get an error message. It says, hey, you can't call a method on a

80
00:05:16,650 --> 00:05:20,340
null valued expression. Big deal, I got an error.

81
00:05:20,340 --> 00:05:23,760
This does not necessarily mean that it's a bad thing. The fact that

82
00:05:23,760 --> 00:05:27,350
I got an error is something that actually I can work within

83
00:05:27,350 --> 00:05:30,840
PowerShell, and I can handle that exception.

84
00:05:30,840 --> 00:05:32,420
However, in Powershell 7,

85
00:05:32,420 --> 00:05:38,620
we now have an alternative, and that is to use this ?. operator.

86
00:05:38,620 --> 00:05:42,750
So the way this works is I reference the variable, and I'm putting the

87
00:05:42,750 --> 00:05:47,560
variable inside curly braces to isolate the variable name because

88
00:05:47,560 --> 00:05:49,900
technically, variable names could include a question mark,

89
00:05:49,900 --> 00:05:51,540
and I need to isolate that.

90
00:05:51,540 --> 00:05:52,500
So I have the variable name,

91
00:05:52,500 --> 00:05:56,390
$p, and then the operator, ?., and then I'm invoking

92
00:05:56,390 --> 00:05:59,000
the method. When I use this operator,

93
00:05:59,000 --> 00:06:01,850
I don't get an error, instead I get a null value.

94
00:06:01,850 --> 00:06:04,960
So you can see I just got a prompt back. And we can

95
00:06:04,960 --> 00:06:07,840
combine these null‑related operators.

96
00:06:07,840 --> 00:06:10,610
So here I'm going to use the ?? operator.

97
00:06:10,610 --> 00:06:13,640
Remember, if the left side of the operation is null,

98
00:06:13,640 --> 00:06:17,910
then use the value that is on the right side of the ??. So

99
00:06:17,910 --> 00:06:22,410
we already know that using the conditional null‑conditional

100
00:06:22,410 --> 00:06:24,940
operator is going to be null,

101
00:06:24,940 --> 00:06:29,090
which means I'm going to get the result, method failed, and that string will

102
00:06:29,090 --> 00:06:32,940
be written to the pipeline, and that gets assigned to $var.

103
00:06:32,940 --> 00:06:40,330
So, PowerShell is running the ?? expression and then returns that result,

104
00:06:40,330 --> 00:06:44,140
and that's what gets assigned to the variable $var.

105
00:06:44,140 --> 00:06:45,420
And so if I look at that,

106
00:06:45,420 --> 00:06:49,140
I get obviously the message that I'm expecting, method failed.

107
00:06:49,140 --> 00:06:52,900
You're more likely to use all these null‑related operators in

108
00:06:52,900 --> 00:06:56,760
PowerShell scripting than in the day‑to‑day basis in the PowerShell

109
00:06:56,760 --> 00:07:03,000
console, but I wanted you to see what they are and so you can get a feel for what they look like.

