Which are the new Null Operators introduced in PowerShell version 7?

PowerShell version 7 has introduced a few new null operators. They are as below.

  • Null-Coalescing operator - ??

  • Null Conditional Assignment Operators - ??=

  • Null Conditional member access operators - ?. and ?[]

a. Null-Coalescing operator - ??

Null Coalescing operator ??evaluates the left-hand side condition or operand and if it is null then evaluates the right-hand side operand otherwise provides the value of the left-hand side operand.

For example, Without the Null-Coalescing operator, we would have written a script like as shown below,

$Name = $null
if($Name -eq $null){"Name is Null"}
Else {"PowerShell"}

The above same condition can be written with the ?? operator.

$name = $null
$name ?? "PowerShell"



So the left side of the variable output is Null and hence right side of the value or expression is evaluated.

Suppose if the left side operand is not null then its value will be displayed.

$name = "Hello"
$name ?? "PowerShell"

You can also add expression,

$service = Get-Service abc -ErrorAction Ignore
$service ?? (Get-Service Spooler)


Status Name DisplayName
------ ---- -----------
Running Spooler Print Spooler

b. Null Conditional Assignment Operator - ??=

Null Conditional Assignment Operator ??= in PowerShell assigns the value of the right-hand side operand to the left-hand operand only if the left-hand operand value is null. This operator doesn’t evaluate the right-hand side operand if the left-hand side operand is Null. For example,

$serivce = $null
$service ??= (Get-Service Winrm)


Status Name DisplayName
------ ---- -----------
Running Winrm Windows Remote Management (WS-Managem…

The above command is similar to,

$service = $null
if($service -eq $null){$service = Get-Service Winrm}
Status Name DisplayName
------ ---- -----------
Running Winrm Windows Remote Management (WS-Managem…

If the left-hand side operator is not null then the value won’t be changed.

$val = "PowerShell"
$val ??= "Hello World"



c. Null Conditional member access operators - ?. and ?[]

As their names suggest, both the operators are used for accessing the members of the object. Both operators were introduced in PS version 7 and still, it is in Preview mode and used for experimental purpose so it is not available to everyone yet.

It is similar to directly accessing any property or member of the variable or an object but why we need them then? Because it evaluates the object first and if it is null then it doesn’t access the members(property or method).

As we are using both operators to access properties or methods of an object, we need to wrap the object around {} brackets, and then we can use the operator. For example,

$service = Get-Service WinRm



The above example can also be achieved by simply access property but the difference is if the service name doesn’t exist then simple command would throw an error where this operator won’t if the service name is null. For example,

$services = Get-Service ABC -ErrorAction ignore
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $service.start()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

The new operator won’t give any output because the service name doesn’t exist.


Updated on: 19-Sep-2020


Kickstart Your Career

Get certified by completing the course

Get Started