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

PowerShellMicrosoft TechnologiesSoftware & Coding

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"

Output−

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"
Hello

You can also add expression,

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

Output−

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)

Output−

$service
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}
$service
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"

Output−

$val
PowerShell

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
${Service}?.Status

Output−

Running

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
$service.start()
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.

${Service}?.Start()
raja
Published on 19-Sep-2020 17:04:00
Advertisements