How to Check if a Variable is Empty in PowerShell?

Recently, I was working with some variables and wanted to check if a variable was empty or contained a value before using it. This is possible using different methods. In this tutorial, I will show you how to check if a variable is empty in PowerShell with some examples.

To check if a variable is empty in PowerShell using the -eq operator, you can compare the variable to $null. For instance, if you have a variable $variable, you can use the following code: if ($variable -eq $null) { Write-Output "The variable is null or empty." } else { Write-Output "The variable is not null or empty." }.

Note: I used Visual Studio Code to execute all the below PowerShell scripts, but you can use any editor.

Check if a Variable is Empty in PowerShell

Now, let me show you different methods to check if a variable is empty in PowerShell with some real examples.

Method 1: Using the $null Comparison

One of the simplest ways to check if a variable is empty in PowerShell is by comparing it to $null. PowerShell treats an uninitialized variable as $null. Here’s an example:

$username = $null

if ($username -eq $null) {
    Write-Host "The username variable is empty."
} else {
    Write-Host "The username variable contains a value: $username"
}

In this example, we initialize the $username variable with $null. We then use an if statement to compare the variable to $null. If the variable is indeed $null, it means it is empty, and we display a message indicating so. Otherwise, we display a message stating that the variable contains a value.

You can see the output in the screenshot below:

Check if a Variable is Empty in PowerShell

Here is another example where you can use the -ne operator to check if the variable is empty.

The -ne (not equals) operator can also be used to check if a variable is not empty. This approach is often preferred when you want to catch edge cases.

$variable = "Hello, World!"

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

Check out Output Variables in PowerShell

Method 2: Using the [string]::IsNullOrEmpty() Method

PowerShell provides a built-in method called [string]::IsNullOrEmpty() that allows you to check if a string variable is empty or $null. Here’s an example:

$address = ""

if ([string]::IsNullOrEmpty($address)) {
    Write-Host "Please provide a valid address."
} else {
    Write-Host "The address is: $address"
}

In this example, we initialize the $address variable with an empty string. We then use the [string]::IsNullOrEmpty() method to check if the variable is empty or $null. If it is, we display a message prompting the user to provide a valid address. Otherwise, we display the address.

Here is the exact output you can see in the screenshot below:

powershell empty variable

Method 3: Truthy/Falsy Evaluation

In PowerShell, variables are evaluated as truthy or falsy in certain contexts, such as conditional statements. An empty string or $null is considered falsy, while a non-empty string or any non-null value is considered truthy. Here’s an example:

$phoneNumber = ""

if ($phoneNumber) {
    Write-Host "The phone number is: $phoneNumber"
} else {
    Write-Host "Please provide a valid phone number."
}

In this example, we initialize the $phoneNumber variable with an empty string. In the if statement, we directly use the variable itself as the condition. If the variable is truthy (non-empty and non-null), the first block will execute, displaying the phone number. If the variable is falsy (empty or $null), the second block will execute, prompting the user to provide a valid phone number.

You can see the exact output in the screenshot below:

if variable is empty PowerShell

Check out PowerShell: Uppercase the First Letter of Each Word

Method 4: Using the -not Operator

The -not operator is another way to check if a variable is empty in PowerShell. This method is useful for readability and simplicity.

$variable = $null

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

Method 5: Using the if Statement Directly

In PowerShell, you can directly use an if statement to check if a string is empty or null. This method evaluates to false if the variable is either null or an empty string.

$variable = ""

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

Here is the output you can see in the screenshot below:

powershell check if empty variable

Conclusion

In this tutorial, I have explained three different methods to check if a variable is empty in PowerShell. I will recommend using the $null comparison method to check if the PowerShell variable is empty.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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