If I got a euro/dollar for every time I had to check whether a value is null, or a variation of it, I would have a nice extra amount in my bankaccount. Why? Because you want to have control in your script to check if a variable is filled or if a command has produced output. In this post, I want to take you through various methods to do this.
Index
Using $Null
Let’s start by checking a variable by comparing it with `$Null`.
## Without creating the variabele
$name -eq $Null
True
## Setting the variabele to $Null
$name = $Null
$name -eq $Null
True
## Setting the variabele to a string
$name = "Test"
$name -eq $Null
False
## Setting the variabele to an empty string
$name = ""
$name -eq $Null
False
## Setting the variabele to a space
$name = " "
$name -eq $Null
False
## Setting the variabele to a int 0
$number = 0
$number -eq $null
False
The above is a good way to start, but as we see here, the check is also $False
for an empty string or a space. This might be fine, but for my purposes, it is often not the right method. Before we look at a better way to check, I first want to highlight another very important point.
$null on the left or right side?
If we check as above, we can get strange/unplanned results. Just look at the following examples.
## $null on the right-side (as the examples above)
if (@() -ne $null) { 'true' } else { 'false' }
false
## $null on the left-side
if ($null -ne @()) { 'true' } else { 'false' }
true
Okay, but what is happening here?
$null
is a scalar value (or single value). When the value on the left side of an operator is a scalar, comparison operators return a Boolean value. When the value is a collection, the comparison operators return any matching values or an empty array if there are no matches in the collection.
So in the above example, the first test actually returns no value, which then results in $false
. Below is another example that illustrates this well.
1,2,3,1,2 -eq $null
## No value returned
1,2,3,1,2 -eq 1
1
1
(1,2,3,1,2 -eq $null).count
0
(1,2,$null,3,$null,1,2 -eq $null).count
2
Besides, it is more readable in your code if you place $null
on the left side. You can immediately see that you are comparing to $null
.
In summary, we can state that for single values, it doesn’t really matter whether you place it on the left or right side of the comparison, other than for readability. But as soon as a variable is or can be a non-scalar value, a comparison requires that we place $null
on the left side. For this reason, I recommend everyone (and with me, psscriptanalyzers) to always place $null
on the left side when comparing with $null
.
String comparisons
As we saw in the first example, when we compare with $null
, we are only checking if the value is $null
. As soon as it is an empty string or just a space, this check no longer works. Now, of course, we can build checks like:
if ($string -eq "") {}
if ($string -eq " ") {}
However, this quickly makes your comparison unnecessarily complex. Instead, we can better check the string using the following methods: IsNullOrEmpty()
or IsNullOrWhiteSpace()
.
IsNullOrEmpty
The [string]::IsNullOrEmpty()
method in PowerShell is a static method. It is used to determine whether a given string is null or empty. In those cases, the result will be $true
. All other values of the string will result in $false
.
The syntax to use this is: [string]::IsNullOrEmpty($string)
To make it clearer, here are some examples:
$String = $null
[string]::IsNullOrEmpty($string)
True
$String = ""
[string]::IsNullOrEmpty($string)
True
$String = " "
[string]::IsNullOrEmpty($string)
False
$String = "value"
[string]::IsNullOrEmpty($string)
False
IsNullOrWhiteSpace
The [string]::IsNullOrWhiteSpace()
method in PowerShell is a static method. It is used to determine whether a given string is null, empty, or consists only of white-space characters. In those cases, the result will be $true
. All other values of the string will result in $false
.
The syntax to use this is: [string]::IsNullOrWhiteSpace($string)
To make it clearer, here are some examples:
$String = $null
[string]::IsNullOrWhiteSpace($string)
True
$String = ""
[string]::IsNullOrWhiteSpace($string)
True
$String = " "
[string]::IsNullOrWhiteSpace($string)
True
$String = "value"
[string]::IsNullOrWhiteSpace($string)
False
As we can see, the [string]::IsNullOrWhiteSpace()
method is more comprehensive. However, the use between the two methods strongly depends on whether the value you want to check may or may not consist only of white-space characters.
Using Typecasting to Boolean
For strings, we have two methods to perform a comparison. But how does this work with arrays? I haven’t found any methods to perform such a check for this type. However, we can misuse typecasting to a boolean to achieve this. Take a look at the examples below.
[boolean](Get-service "NonExistingService")
## ERROR: Get-Service: Cannot find any service with service name 'NonExistingService'.
False
[boolean](Get-service "NonExistingService" -ErrorAction SilentlyContinue)
False
[boolean](Get-service "wuauserv")
True
Recap
Checking for empty or null values is a good addition to your code. It helps prevent many errors and gives you more control over the output. And remember to always place $null
on the left side of the comparison.
How about you? Are there any interesting ways you make such comparisons? Let me know in the comments. Also, feel free to ask if you have any questions or uncertainties.
Leave a Reply