Next, let's take a look at some new PowerShell 7 operators that were
designed to make it easier to work with null values.
These will make your head hurt a little bit, and they will
take some time to get your head around.
And I encourage you, again, to try the demos that I have in the course
downloads so you can see how these things work for yourself.
But at least let me give you a quick introduction to these operators.
So I'm going to create a variable, $n, and basically
assign a null value to it using $null,
a special built‑in variable in PowerShell. The
null‑coalescing operator is the two question marks.
So if the left side of that operator is null,
then evaluate or run the code that's on the right side.
So if $n is null, then use the Write‑Warning command and say,
hey, show me that the variable is null,
which is exactly the result that I am expecting.
Here's another example.
So I'm going to assign the result of the expression
$PSEdition ??, null‑coalescing,
I'm not quite sure what a good way to describe the the two
question marks is yet. If the left side is not null,
then the right side is never used.
So, $PSEdition should exist on all computers. And so if it does exist,
PowerShell will assign that value to $v. Otherwise, if for some
reason it doesn't exist, then unknown will be assigned to the value
of $v. And because $PSEdition exists,
then $v has a value, in this case, of Core because I'm
running this on a PowerShell 7 system.
So this null‑coalescing operator becomes a handy shortcut way
comparing values to see if it's null or not and then deciding
what else to do, very quick, simple,
handy way. Related to that is the null‑coalescing assignment operator.
That's the two question marks and the equal sign.
So in this situation, we're going to assign the value from the right‑hand
side to the left‑hand side if the left‑hand side is null.
So $n is assigned the value of null. If $n is null,
then we're going to give it a value of foo.
So now, if I were to look at $n, I would see that it has a
value of foo, and sure enough, it does.
So let's rerun that command, $n, if it's null, then assign a value
of bar. What do you think the value of $n is now, considering what I
just did a moment ago? If you guessed foo, you are correct. Because
$n was null to begin with,
I assigned it a value of foo because $n was null, $n is no longer null,
so the bar command never runs.
So let's look at this from a more practical perspective. I'm going to create
a variable, $p, and I want that value of p to be all the processes that have
a working set size of greater than 1 gigabyte.
If $p is null, if that Get‑Process command doesn't return anything,
then $p will have null, in which case, I want to assign a value of 0.
So because the Get‑Process command resulted in no processes, the
null‑coalescing assignment command ran successfully, assigned a value of
0 to $p, and you can see that $p has a value of 0.
The last null‑related operator I want to talk about is the question mark period.
And this is an operator that we use when we're trying to access members
of an object, and specifically, methods of an object.
So I'm going to, just to show you first how things normally work, and
then we'll show how it would work when things are null.
So I run Get‑Process for an existing process such as the current
PowerShell process and save that to a variable $p, and now I want
to invoke a method of an object.
All objects have a ToString method, so this is a safe thing to test with.
So if I do $p.ToString(),
and remember when you invoke methods, you need to include the
parentheses, I get the result that I expect.
And this works because $p is not null.
But, what if $p was null? Now in my demo here, I'm just
going to assign a value of null to $p.
But maybe in the real world, you forgot to create $p, or maybe
you're using $p that was defined by some external process that
failed to create it, and you have no idea.
And this happens a lot in PowerShell. We work with things that maybe I got a
result, maybe I didn't, let's see. And again, these are things that you'll
probably work with more when you get to scripting,
but we can work with this in the console as well.
So I have $p. And in this particular case, it's
null, but maybe I don't know that.
Now if I try to invoke that same method,
I get an error message. It says, hey, you can't call a method on a
null valued expression. Big deal, I got an error.
This does not necessarily mean that it's a bad thing. The fact that
I got an error is something that actually I can work within
PowerShell, and I can handle that exception.
However, in Powershell 7,
we now have an alternative, and that is to use this ?. operator.
So the way this works is I reference the variable, and I'm putting the
variable inside curly braces to isolate the variable name because
technically, variable names could include a question mark,
and I need to isolate that.
So I have the variable name,
$p, and then the operator, ?., and then I'm invoking
the method. When I use this operator,
I don't get an error, instead I get a null value.
So you can see I just got a prompt back. And we can
combine these null‑related operators.
So here I'm going to use the ?? operator.
Remember, if the left side of the operation is null,
then use the value that is on the right side of the ??. So
we already know that using the conditional null‑conditional
operator is going to be null,
which means I'm going to get the result, method failed, and that string will
be written to the pipeline, and that gets assigned to $var.
So, PowerShell is running the ?? expression and then returns that result,
and that's what gets assigned to the variable $var.
And so if I look at that,
I get obviously the message that I'm expecting, method failed.
You're more likely to use all these null‑related operators in
PowerShell scripting than in the day‑to‑day basis in the PowerShell
console, but I wanted you to see what they are and so you can get a feel for what they look like.