Powershell – Variable for Null, Empty String and White Space

More often than not, we create variables with the intent they will hold a value. We typically then choose to act upon that value. Therefore, if a variable doesn’t have value, this is something we would want to check against in our conditional logic flow.

However, not having a value may mean a couple of different things:

Our variable may truly be absent of a value, and therefore its “value” is $null, an Automatic Variable in PowerShell which can “represent an absent or undefined value in commands and scripts.” This could apply to any variable that we declare but for which we do not set a value.

If our variable is a string, it could contain a value of “”, which is an empty value as far as a string is concerned, but is still a value when compared to $null.
Let’s look at how to test for these conditions.

Test PowerShell variable for $null

Let’s open a fresh ISE session. Using the following code, we are declaring a variable $DemoString but not assigning it a value. We then immediately test it to see if it is $null.

We use $DemoString -eq $null:

PS C:\Users\aaron> $DemoString
$DemoString -eq $null
True

Test PowerShell variable for empty string value

Expanding on our original demo code, let’s now test the same variable to see if $null is equivalent to an empty string value, which is represented as “”:

PS C:\> $DemoString
PS C:\> $DemoString -eq $null
True
PS C:\> $DemoString -eq ""
False

You can see from this that $DemoString with no value assignment tests true for $null, but NOT for an empty string “”. To further confirm this, we can test $null against “” directly:

PS C:\> $null -eq ""
False

This fails because even though the empty string value doesn’t seem like a true value, it is in fact a legitimate string type value.

IsNotNullOrEmpty

If the value is a string, you can use a static string function to check if the value is $null or an empty string at the same time.

if ( -not [string]::IsNullOrEmpty( $value ) ){...}

You can use this often when you know the value type should be a string.

Test PowerShell variable for either $null or empty string value

Now let’s look at how you might use this knowledge when scripting. We now know there are two distinct conditions that we need to potentially test for before attempting an operation on a variable.

In this example, we are going to use Get-CimInstance to return information about a specified Windows service. In this particular case, we are checking whether or not the variable holds an object. In the first command, we get WinRM, a valid service. In the second command we attempt to get FakeService, which doesn’t exist.

PS C:\> $Service = Get-CimInstance -ClassName Win32_Service -Filter "Name='WinRM'"
$Service -eq $null
False

PS C:\> $Service = Get-CimInstance -ClassName Win32_Service -Filter "Name='FakeService'"
$Service -eq $null
True

You can see that $Service is NOT $null in the first command, but IS $null in the second command. Straightforward.

What if I initialize the $b variable, but I do so with an empty string?

PS C:\> $b = " "
PS C:\> [string]::IsNullOrEmpty($b)
False

This time, our empty string comes back and says that it is not null, nor is it empty. It contains a blank space. What if we remove that space?

PS C:\> $b = ""
PS C:\> [string]::IsNullOrEmpty($b)
True

It seems that removing the space caused it to be null or empty.

Well, what about white space like the one I had earlier with the $b? As shown here, there is a method called IsNullOrWhiteSpace:

PS C:\> $c = " "
PS C:\> [string]::IsNullOrWhiteSpace($c)
True

These commands and their output are shown here:

Powershell - Variable for Null, Empty String and White Space

$null in a Collection

A collection allow you use an index to access values. If you try to index into a collect that is actually null, then you will get this error: Cannot index into a null array.

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

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

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

$null.Count

If you try to access a property on a $null value, that the property will also be $null. The count property is the exception to this rule.

    PS> $value = $null
    PS> $value.count
    0

When you have a $null value, then the count will be 0. This special property is added by PowerShell.

$null in numeric equation

When a $null value is used in a numeric equation then your results will be invalid if they don’t give an error. Sometimes the $null will evaluate to 0 and other times it will make the whole result $null. Here is an example with multiplication that gives 0 or $null depending on the order of the values.

    PS> $null * 5
    PS> $null -eq ( $null * 5 )
    True

    PS> 5 * $null
    0
    PS> $null -eq ( 5 * $null )
    False