Null Variables in PowerShell

  1. Introduction to Null Variable in PowerShell
  2. Impacts of the Null Variable in PowerShell
  3. Checking for Null Values in PowerShell
Null Variables in PowerShell

PowerShell treats the $Null object with the value null, and some commands require some output to generate. They generate value to null if there is any error and can also be helpful in the troubleshooting purpose in the scripts to check if the command generates any value.

This article talks about the null variable, the impacts of using the null variable in different syntax in PowerShell, and ways of checking the null values.

Introduction to Null Variable in PowerShell

We can think of NULL as an unknown or empty value. For example, a variable is NULL until you assign a value or object.

This variable can be important because some commands require a value and generate errors if the value is NULL.

The $null variable is an automatic variable in PowerShell used to represent NULL. We can assign it to variables, use it in comparisons and use it as a placeholder for NULL in a collection.

PowerShell treats $null as an object with the NULL value. Therefore, this variable is different then what you may expect if you come from another language.

Impacts of the Null Variable in PowerShell

$null values will impact your code differently depending on where they appear.

Impact on Strings

If you use $null in a string, it will be a blank value (or empty string).

$value = $null
Write-Output "The value is $value."

Output:

The value is .

This variable is one of the reasons that we have to place brackets around variables when using them in log messages. It is even more critical to identify the edges of your variable values when the value is at the end of the string.

$value = $null
Write-Output "The value is [$value]"

Output:

The value is []

This variable makes empty strings and $null values easy to spot.

Impact on Numerical Equations

When a $null variable is used in a numeric equation, our results will be invalid if they do not give an error. Sometimes the $null variable will evaluate to 0, and other times it will make the whole result $null.

Here is an example that involves multiplication that gives 0 or $null depending on the order of the values.

$null * 5
$null -eq ( $null * 5 )

Output:

True

Impact on Collections

A collection allows us to use an index to access values. If we try to index into a null collection, we will get this error: Cannot index into a null array.

$value = $null
$value[10]

Output:

Cannot index into a null array.
At line:2 char:1
+ $value[10]
+ ~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

We have a collection but try to access an element that is not in the collection, and then you will get a $null result.

$array = @( 'one', 'two', 'three' )
$null -eq $array[100]

Output:

True

Impacts on Objects

If we try to access a property or sub-property of an object with no specified property, we can get a $null variable like you would for an undefined variable. It does not matter if the value is $null or an actual object in this case.

$null -eq $undefined.not.existing

Output:

True

Impacts on Methods

Calling a method on a $null variable will throw a RuntimeException exception.

$value = $null
$value.toString()

Output:

You cannot call a method on a null valued expression.
At line:2 char:1
+ $value.toString()
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Whenever we see the phrase You cannot call a method on a null valued expression, we can look for places to call a method on a variable without first checking it for $null values.

Checking for Null Values in PowerShell

We always place the $null variable on the left when checking for $null values in the examples.

This style of writing is intentional and accepted as a PowerShell best practice. However, there are some scenarios where placing it on the right will not give you the expected result.

Let us look at the following example and try to predict what the results will be:

if ( $value -eq $null ){
    'The array is $null'
}

if ( $value -ne $null ){
    'The array is not $null'
}

If we do not define $value, the first one will evaluate $true, and our message will be The array is $null. The caveat here is that creating a $value that will allow both results to be $false is possible.

Output:

$value = @( $null )

In this case, the $value variable is an array that contains a $null.

The -eq operator will check every value in the array and returns the $null that is matched (This evaluates to $false). The -ne operator will return everything that does not match the $null value, and in this case, there are no results (This will also evaluate to $false).

So neither one will be $true even though one of them should be.

Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - PowerShell Variable