Powershell

What does $null mean in PowerShell?

In PowerShell, $null is an object that carries a value null, and can be used for different purposes. The $null value can be used in the strings to add blank spaces, it can be used in arrays as an empty placeholder, etc. In PowerShell, the undefined variables return a null value. Moreover, a $null value can be assigned to the variables and can be used with the collections, or in the comparisons.

This write-up will present a detailed understanding of the $null variable in PowerShell with the below-listed learning outcomes:

So, let’s begin!

What is $null in PowerShell?

It is an automatic variable that carries a NULL or unidentified value. Unlike other programming languages, Windows PowerShell considers $null as an object that carries a Null value.

How does the $null variable Work in PowerShell?

Up till now, we have understood what is $null in PowerShell. Now it’s time to understand how the $null variable works in PowerShell. To do that we will consider some examples.

Example1

To get a basic understanding of the $null variable, let’s type $null and see what will be the resultant output:

$null

In such a case, the $null variable will produce nothing as shown in the following snippet:

The output shows that the $null variable produces nothing.

Example2

Let’s consider the following snippet to understand what will be the default value of a variable in PowerShell:

$empName

$empName -eq $null

In this example, we created a variable named “empName” and assigned it nothing. In the next line, we utilized the “-eq” operator to compare the value of the empName variable with the $null variable. As a result, we will get the following output:

The output shows that if we didn’t initialize a value to a variable, then by default the value of that variable will be null.

Example3

How to assign a null value to a variable explicitly:

$empName = $null

Write-Output "Resultant Value: $empName"

In the above snippet, we created a variable named “empName ” and assigned it a “null” value. Afterward, we utilized the Write-Output cmdlet to show the output:

The output shows nothing which authenticates that the variable $empName holds a Null value.

Difference Between $null and “”

In PowerShell, $null and “”, both represent an empty value, however, they are not the same/equal.

Example

The below given example will let you understand that the $null and “” are not equal:

"" -eq $null

In this example, we compared “” with the $null value using the “-eq” operator, consequently, we get the following output:

The output shows that both “” and $null are not equal.

In PowerShell, when we assign “” to a variable it means we are assigning a blank space to a variable, on the other hand, if we assign $null to a variable it means we are assigning nothing/undefined value to that variable. Consider the below example for the clarity of this concept:

Example2

In the below code snippet, we will create two variables:

$empName = ""

$empName1 = $null

$empName -eq $empName1

In this example, we created two variables empName, and empName1. Next we initialized “” to empName and $null to empName1. Finally, we utilized the “-eq” operator to compare the value of empName with empName1:

The output shows that “” and $null are two different things.

Use of Null Value with Functions

In PowerShell, if a method doesn’t return anything it means it will return a null value. Moreover, a variable called from out of scope will return a null value.

Example

In the following code block, the method “myFun” will return nothing:

Function myFun{}

$result = myFun

$result -eq $null

In the above snippet, we created a function that returns nothing. Next, we compared the function’s return value with the “$null”. As a result, we will get the following output:

The output shows a “true” value, it verifies that when a method doesn’t return anything it means the method returns a $null value.

Example2

In this example, we will understand what will be output when we try to access an out of scope variable:

PS C:\Users\DELL> Function scopeExample($var1){

>> Write-Output "Value of First Variable : $var1"

>> $var2 = 100

>> }

>> $var1 = 210

>> scopeExample $var1

>> Write-Output "Value of Second Variable : $var2"

The above code will generate the following output:

The above snippet shows that when we try to call the variable from out of the scope then it returns a null value.

Use of $null Value with Numeric Equations

In numeric equations, mostly the $null value serves as 0. While sometimes the behavior of the $null value depends on the equation’s order. When we use the $null value with the numeric equations then sometimes we will get the result as 0 and sometimes we will get the result null (mostly in the case of multiplication), it depends on the equation’s order.

Example

The below code block will provide you more clarity about the working of numeric equations:

$null + 50

100 + $null

10 - $null

$null -100

The above snippet shows that the $null value acts as 0 in the above example regardless of the order.

Example2

The below code snippet will explain the working of $null value with respect to multiplication and division:

$null * 100

100 * $null

100 / $null

$null / 100

The output shows that the $null value acts as 0 in all the cases except the first equation(i.e. $null * 100).

Use of Null Value with Arrays

While working with arrays if we try to access/call an out of range index then the resultant value will be $null:

Example

Consider the below snippet to understand how $null value will be treated with arrays:

$name = "Alex", "John", "Mike", "Joe"

$name[$null]

The output shows an exception which means arrays don’t $null value as 0 instead.

Example2

Let’s consider the below example to check whether the out of range index will be treated as null or not:

$name = "Alex", "John", "Mike", "Joe"

$name[4] -eq $null

In the above code, the array “$name” has four elements. We invoked the fourth index and compared the resultant value with the $null value:

Output shows a true value, which means arrays treat the out of range index as a $null value.

Conclusion

In PowerShell, $null is an automatic variable that carries a NULL or unidentified value. Windows PowerShell considered $null as an object that carries a Null value. The $null value can be used with numeric equations, arrays, functions, etc. Sometimes it acts like 0, while sometimes it serves differently (i.e. as a null value or undefined value). This post explained different aspects of the $null value with the help of some appropriate examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.