Home » How to Check if Variable is Null or Empty in PowerShell

How to Check if Variable is Null or Empty in PowerShell

In PowerShell, you can check if a variable is null or empty by using the -eq $null comparison for null values and the -eq “” comparison for empty strings.

The following methods show how you can do it.

Method 1: Check if a Variable is null

$variable -eq null

This example checks if the $variable is null using the -eq comparison operator.

Method 2: Check if a variable is not null

$variable -ne $null

This example checks if variable $variable is not null using the -ne comparison operator.

Method 3: Check if a string variable is empty

$stringData -eq ""

This example checks if variable $stringData is empty using -eq “”.

Method 4: Check if a string is not empty

$stringData -eq ""

This example checks if variable $stringData is empty using -eq “”.

Method 5: Check if a variable is null or empty

$variable -eq $null -or $variable -eq ""

This example checks if a variable $variable is null or empty.

Method 6: Check if a variable is neither null nor empty

$variable -ne $null -or $variable -ne ""

This example checks if a variable is not null or not empty.

The following examples show how you can use these methods.

Check if a Variable is Null in PowerShell

You can check if a variable is null in PowerShell using the -eq comparison operator with $null.

# define the variable
$number = $null
if($number -eq $null) {
    Write-Output "Variable is null"
}
else{
   Write-Output "Variable is not null."
}

Output:

Variable is null

In this script, if statement checks the $number is $null by comparing it with the $null constant and outputs whether the variable is null or not null.

Check If a Variable is Not Null in PowerShell

You can check if a variable is not null in PowerShell by using the -ne comparison operator.

# define the variable
$number = 10
if($number -ne $null) {
    Write-Output "Variable is not null."
}
else{
   Write-Output "Variable is null."
}

Output:

Variable is not null.

In this script, we define a variable $number and assign it a value of 10. We then use the -ne comparison operator within the if statement to check if the $number is not equal to $null.

Since the $number is not null, the output will be “Variable is not null“.

Check If a String Variable is Empty in PowerShell

You can check if a string variable is empty in PowerShell by using the -eq comparison operator and comparing it against the “” (empty).

# define the variable
$number = ""
if($number -eq "") {
    Write-Output "Variable is empty."
}
else{
   Write-Output "Variable is not empty."
}

Output:

Variable is empty,

In this script, we define a variable $number and assign it a value of “” (empty). We then use the -eq operator within the if statement to check if the variable $number is equal to empty.

Since the $number is assigned with “”, the output will be “Variable is empty.”.

Check if a String Variable is Not Empty in PowerShell

You can check if a string variable is not empty by using the -ne operator and comparing it with “” (empty).

# define the variable
$number = 20
if($number -ne "") {
    Write-Output "Variable is not empty."
}
else{
   Write-Output "Variable is empty."
}

Output:

Variable is not empty.

In this script, the $number variable is compared with “” (empty) using the -ne operator. Since the $number variable contains the value in it, it will output “Variable is not empty.

Check If a Variable is Null or Empty in PowerShell

You can check if a variable is null or empty in PowerShell by using the -eq comparison operator to compare with $null or “” (empty).

# define the variable
$number = 15.55
if($number -eq $null -or $number -eq "") {
    Write-Output "Variable is null."
}
else{
   Write-Output "Variable is not null."
}

Output:

Variable is not null.

In this script, we define a variable $variable and assign it a value of 15.55. We then use the if statement to check if the $variable is equal to $null or equal to “” (empty).

Since the $variable contains a 15.55 value, the output will print the message as “Variable is not null.

Check if the Variable is Neither Null nor Empty in PowerShell

You can check if a variable is null or empty in PowerShell by using the -ne comparison operator to compare with $null or “” (empty).

# define the variable
$number = $null
if($number -ne $null -and $number -ne "") {
    Write-Output "Variable is not null."
}
else{
   Write-Output "Variable is null."
}

Output:

Variable is null.

In this script, we define a variable $variable and assign it a value of $null. We then use the if statement to check if the $variable is equal to $null or equal to “” (empty).

Since the $variable contains a $null value, the output will print the message as “Variable is null.

Conclusion

I hope the above article on checking if a variable is null or empty in PowerShell is helpful to you.

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