Updated March 6, 2023
Introduction to PowerShell null
The following article provides an outline for PowerShell null. PowerShell $Null is an automatic variable that holds nothing but unidentified or absent value and it is also considered as an Object. PowerShell treats $Null object with the value null and some commands require some output to generate and they generate value null if there is any error and can also be useful in the troubleshooting purpose in the scripts to check if the command generates any value.
Syntax:
Although there is no specific syntax for null, we can use the automatic and predefined variable $NULL for this.
How does $null Variable Works in PowerShell?
When you type $Null, it produces nothing as shown below.
Code:
$null
When you declare a variable and assign nothing, the value by default it holds is the $NULL.
Code:
$a
$a -eq $null
Output:
You can also assign $null value explicitly to a variable as shown below.
Code:
$val = $null
Write-Output "The value is : $val"
Output:
Many times when writing a script we need to spot if the value is Null but it doesn’t visible in the console. To spot if the variable is going to hold a null value, we need to use the special characters or something that we can identify that the variable holds a null value. We will use it here {}. You can use anything for it like other brackets.
Code:
$val = $null
Write-Output "The value is : {$val}"
Output:
Examples of PowerShell null
Given below are the examples of PowerShell null:
Example #1
Difference between “” and $NULL.
Although both (“” and $NULL) represent the empty value there is a quite difference between them.
The first thing is both are not equal.
Code:
"" -eq $null
Output:
When you declare a variable without any value, its output is $NULL.
Code:
$a
$a -eq $null
Output:
But when you compare it with the “” value, the output will be false.
Code:
$a -eq ""
Output:
“” represents the empty space. This means when you assign that value to the variable, its output will be one blank line while $null output is nothing.
Code:
$a = ""
$a
Output:
Now $a is not $null.
Code:
$a -eq $null
Output:
Another way to check this is with the .NET method that the $null value is not similar to “” using String class.
Code:
$null -eq [String]::Empty
Output:
Example #2
Null is an Undefined variable.
Any variable whose values are not initialized is considered as the undefined variable.
In this example, $service is not defined earlier so its value is not initialized and it is considered Null.
Code:
$null -eq $service
Output:
Example #3
Null with Function.
When the function returns nothing then its output is Null.
Code:
Function ReturnsNull{}
$val = ReturnsNull
$val -eq $null
Output:
The above output is true because the function doesn’t return anything.
The above example is similar to the below examples because they don’t return anything.
Code:
Function ReturnsNull{ return }
$val = ReturnsNull
$val -eq $null
Output:
Another example of function.
Code:
Function ReturnsNull{
$svc = Get-Service NonExist -EA Ignore
if($svc){ return $svc }
}
$val = ReturnsNull
$val -eq $null
Output:
Here, service doesn’t exist so the function returns Null.
Example #4
Null values for the PowerShell function scope.
When variables are called outside their scope, their values are always null because they are uninitialized values for the outside scope.
Code:
Function TestScope($a){
Write-Output "Value of a : $a"
$b = 20
}
$a = 10
TestScope $a
Write-Output "Value of b : [$b]"
Output:
In the above example, the value of $a variable is known to the function TestScope because it was declared outside the function but $b value is declared inside the function so that is unknown to the outside of the function and hence the value of $b variable shows NULL. To get the $b value outside of the function, you need to use the return command and need to capture the value from the function. This also works when you define $b as a global variable outside of the function and change its value in the scope.
Example #5
Null with numeric equation.
When we use the $null value with the numeric equation, it behaves similarly to the Value 0.
Code:
$null + 1
$null + 5 + 10
4 + $null
Output:
For multiplication, it depends on the position of the $Null value.
Code:
$null * 4
4 * $null
Output:
It works the same for the divide operation and $null is considered as 0.
Code:
$null / 10
10 / $null
Output:
Example #6
Null value with array.
When we call the index out of the bound of an array, it gives the null output.
Code:
$Colors = "Red","Yellow","Pink"
In this example, we have an array with 3 values, and if we call Colors array with the index 4, it will give a null value because that index doesn’t exist.
Code:
$Colors[4]
$Colors[4] -eq $null
Output:
In the array case, $null is not treated as 0. It generates an exception when it is used for an index.
Code:
$Colors[$null]
Output:
Conclusion
Every variable assignment and function return values start with null until they are defined. They are negligible for any script because it doesn’t show any output for it but can be a big threat to a script if you are using the value of the one output to another one and the output is null. For large scripts, troubleshooting can be cumbersome if the null is not treated properly.
Recommended Articles
This is a guide to PowerShell null. Here we discuss the introduction, how does $null variable works in PowerShell? and examples respectively. You may also have a look at the following articles to learn more –