Home » PowerShell » PowerShell $null – Check for null

PowerShell $null – Check for null

PowerShell $null is an automatic variable that represents null or absent.PowerShell $null is used as an empty placeholder to assign it to a variable, use it in comparisons.

PowerShell $null is an object with null or absent value.

NULL is used to represent empty or undefined. Variable hold null until you assign any value to it.

In this example, we will discuss PowerShell $null, check if a variable is empty, or if not null.

Using PowerShell null check

$null

When you type $null on the console, it returns empty.

Check with another example

$Title
$null -eq $Title

The above command performs PowerShell null check, it returns TRUE.

PowerShell null check
PowerShell null check

Cool Tip: How to use PowerShell Exit keyword!

Using PowerShell $null value

You can assign PowerShell $null to a variable.

$Title = $null
Write-Output "The Title is:$Title"

This command prints empty or blank for $Title.

The output of the above command is:

PS C:\> $Title = $null                                                                           PS C:\> Write-Output "The Title is:$Title"                                                       The Title is:
PS C:\>                                                                                                   

If you want to spot the $null for variable, use it within the bracket.

Write-Output "The Title is:[$Title]"

This command displays output and placeholder for the $Title within the bracket.

We have used [] here.

You can use anything for it like {} or other brackets.

The output of the above command is:

null in PowerShell
null in PowerShell

Using PowerShell $null to check Undefined

You can use $null to check an undefined variable.

If the variable is not initialized with a value, it is undefined.

$null -eq $Author

This command checks if $Author is null in PowerShell.

It returns the TRUE value as it is not initialized.

Cool Tip: How to replace text in string in PowerShell!

Using $null in numeric equation

$null variable in numeric equation evaluates to 0 or $null based on the order of its use in an expression.

PS C:\> 10 * $null                                                                               
0
PS C:\>                                                                                                       

In the above example, the $null variable evaluates to 0 and results in 0.

PS C:\> $null * 10
PS C:\> $null -eq ($null * 10)

In this example, the $null variable evaluates to null.

It returns TRUE.

Cool Tip: Do you know how to print environment variables in PowerShell!

Using PowerShell $null in Function

When we use the function in PowerShell, it may return output or nothing.

Use PowerShell $null to check if the function in PowerShell returns null.

Function ReturnFactorial(){}

$fact = ReturnsFactorial

$null -eq $fact

In the above example,

ReturnFactorial() function in PowerShell returns nothing ,assign it to $fact and check if null.

It returns a TRUE.

PowerShell null check -function
PowerShell null check -function

Cool Tip: Do you know how to add a newline to string or variable in PowerShell?

Using PowerShell $null with Array

An array contains the list of values in it.

To access the value of the array or collection, we use the index.

If we try to use an index into an array or collection which doesn’t hold any value, it will null.

$languages  = @('C#','PowerShell','Java')
$languages[4]

In the above example, the $languages contain an array of size 3.

We try to access an element at position 4 which is not available in the array hence it will get a $null result and throw an exception.

To avoid error while accessing index in collection, check if it contains $null.

PS C:\> $languages  = @('C#','PowerShell','Java')
PS C:\> $null -eq $languages[4]
TRUE

Cool Tip: PowerShell String Concatenation with examples!

Conclusion

The above article outlines Powershell $null with example.

Its best practice in PowerShell to check if a variable is a null or empty value before using it.

Cool Tip: PowerShell echo equivalent command in Windows!

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.