How to Check if a Variable is Null in PowerShell?

Recently, one of my subscribers asked me about checking if a variable is null in PowerShell. This is a very common requirement if you are working with PowerShell. In this tutorial, I will explain different methods to check if a variable is null in PowerShell, with examples and complete scripts.

To check if a variable is null in PowerShell, you can use the -eq (equals) operator. For example, if you have a variable $variable, you can write:

if ($variable -eq $null) {
    Write-Output "The variable is null."
} else {
    Write-Output "The variable is not null."
}

This method compares the variable directly to $null and outputs whether it is null or not.

What is Null in PowerShell?

In PowerShell, $null represents the absence of a value or an undefined value. It is similar to null in other programming languages like C# or JavaScript. When a variable is assigned $null, it means that it does not contain any data.

Check if a Variable is Null in PowerShell

Now, let us check all the methods to check if a variable is null in PowerShell.

1. Basic Null Check

The most straightforward way to check if a variable is null in PowerShell is to use the -eq (equals) operator. Here is an example.

$variable = $null
if ($variable -eq $null) {
    Write-Output "The variable is null."
} else {
    Write-Output "The variable is not null."
}

In this example, since $variable is assigned $null, the condition $variable -eq $null evaluates to True, and the script outputs “The variable is null.”

The output is in the screenshot below. I executed the above script using VS code.

Check if a Variable is Null in PowerShell

Check out PowerShell Global Variables

2. Using -ne Operator

In PowerShell, you can also use the -ne (not equal) operator to check if a variable is not null. This is another method to check if a variable is null in PowerShell. Here is an example below:

$variable = "PowerShell"
if ($variable -ne $null) {
    Write-Output "The variable is not null."
} else {
    Write-Output "The variable is null."
}

Here, $variable is a string, so the condition $variable -ne $null evaluates to True, and the script outputs “The variable is not null.”

You can see the output in the screenshot below:

PowerShell Check if a Variable is Null

3. Combine Null and Empty String Check

Sometimes, you may want to check if a variable is either null or an empty string in PowerShell. This is particularly useful when dealing with user input or file paths. You can combine these checks using the -or operator:

$variable = ""
if ($variable -eq $null -or $variable -eq "") {
    Write-Output "The variable is either null or empty."
} else {
    Write-Output "The variable is not null and not empty."
}

In this case, since $variable is an empty string, the condition $variable -eq $null -or $variable -eq "" evaluates to True, and the script outputs “The variable is either null or empty.”

4. Using IsNullOrEmpty Method

PowerShell provides a static method IsNullOrEmpty() method from the [string] class to check if a string is null or empty.

$variable = ""
if ([string]::IsNullOrEmpty($variable)) {
    Write-Output "The variable is either null or empty."
} else {
    Write-Output "The variable is not null and not empty."
}

This approach is particularly useful when working with strings and ensures that your checks are both efficient and easy to understand.

5. Null-Coalescing Operators (PowerShell 7+)

Starting from PowerShell 7, you can use the null-coalescing operators ?? and ??= to handle null values more elegantly. The ?? operator returns the right-hand side value if the left-hand side is null.

Here is the complete script.

$variable = $null
$default = "Default Value"
$result = $variable ?? $default
Write-Output $result  # Outputs "Default Value"

The ??= operator assigns a value to a variable only if it is null:

$variable = $null
$variable ??= "Default Value"
Write-Output $variable  # Outputs "Default Value"

These operators provide a more modern way to handle null values in PowerShell.

Check out PowerShell Local Variables

Check if a Variable is Null in PowerShell: Real-World Examples

Now, let me show you some real-world examples in PowerShell to check if a variable is null.

Example 1: Checking User Input

Suppose you are writing a script to collect user input for a survey. You want to ensure that the input is not null or empty before processing it. Then you can write the script like below:

$userInput = Read-Host "Enter your feedback"
if ([string]::IsNullOrEmpty($userInput)) {
    Write-Output "Feedback cannot be empty. Please try again."
} else {
    Write-Output "Thank you for your feedback!"
}

Here is the output of the PowerShell script in the screenshot below:

Check if a Variable is Null in PowerShell Examples

Example 2: Validating File Paths

In a script that processes files, you may want to ensure that the file path provided by the user is not null or empty. You can have the PowerShell script like below:

$filePath = Read-Host "Enter the path to the file"
if ([string]::IsNullOrEmpty($filePath)) {
    Write-Output "File path cannot be empty. Please provide a valid path."
} elseif (-Not (Test-Path $filePath)) {
    Write-Output "File does not exist. Please check the path and try again."
} else {
    Write-Output "Processing file: $filePath"
    # Add file processing logic here
}

Conclusion

There are many scenarios in PowerShell where you might need to check if a variable is null, such as validating user input, processing files, handling configuration settings, etc. In this tutorial, I have explained different methods for checking if a variable is null in PowerShell. I always recommend using the—ne Operator for this.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.