While working on PowerShell scripts, you’ll often need to check if a variable contains data before performing operations on it. There are various methods for checking this. In this article, I’ll show you several methods to check if a PowerShell variable is null or empty. Let us check various approaches and see which one works best for different scenarios.
To check if a variable in PowerShell is null or empty, you can use the -eq operator or the [string]::IsNullOrEmpty() method. For example, $variable -eq $null or [string]::IsNullOrEmpty($variable) will return True if the variable is null or an empty string, respectively. If you need to account for strings that contain only whitespace, use [string]::IsNullOrWhiteSpace($variable) instead.
Method 1: Using the -eq $null Operator
The best way to check if a variable is null is to compare it directly with the $null special value in PowerShell.
Here is the PowerShell script.
$myVariable = $null
if ($myVariable -eq $null) {
Write-Host "The variable is null"
} else {
Write-Host "The variable is not null"
}
Note that the $null value should always be on the right side of the comparison. This is because PowerShell evaluates the left side first, and if you reverse the order with $null -eq $myVariable, it might not behave as expected with arrays.
You can see the exact output in the screenshot below:

Check out Export Arrays to CSV in PowerShell
Method 2: Using the -not Operator
Another common approach is to use the -not operator, which effectively checks if a variable has a value. Here is the PowerShell script to check if the variable is null or empty.
$myVariable = $null
if (-not $myVariable) {
Write-Host "The variable is null or empty"
} else {
Write-Host "The variable has a value"
}
This method checks both for null values and empty strings, which makes it more versatile than just checking for null.
You can see the output in the screenshot below:

Check out How to Convert Date to String in PowerShell
Method 3: Using [String]::IsNullOrEmpty()
When specifically dealing with string variables, the .NET method [String]::IsNullOrEmpty() is perfect for checking if a string is null or empty.
$myString = ""
if ([String]::IsNullOrEmpty($myString)) {
Write-Host "The string is null or empty"
} else {
Write-Host "The string has content"
}
This method is particularly useful because it handles both null strings and empty strings in one check.
You can see the exact output in the screenshot below:

Check out PowerShell Write to File UTF-8
Method 4: Using [String]::IsNullOrWhiteSpace()
If you want to check for strings that contain only whitespace characters (spaces, tabs, etc.), use the [String]::IsNullOrWhiteSpace() method:
$myString = " "
if ([String]::IsNullOrWhiteSpace($myString)) {
Write-Host "The string is null, empty, or contains only whitespace"
} else {
Write-Host "The string has non-whitespace content"
}
This is extremely useful when validating user input, where spaces alone shouldn’t be considered valid data.
Method 5: Using Try-Catch with Undefined Variables
Sometimes you need to check if a variable exists at all. In this case, you can use a try-catch block:
try {
$value = Get-Variable -Name "NonExistentVariable" -ValueOnly -ErrorAction Stop
Write-Host "Variable exists with value: $value"
} catch {
Write-Host "Variable does not exist"
}
Check out PowerShell Array of Strings
Real-World Example: Processing Customer Data
Let’s look at a practical example where checking for null or empty values is essential. Imagine we’re processing customer data from a CRM system for a US-based retail company:
function Process-CustomerData {
param (
[string]$FirstName,
[string]$LastName,
[string]$Email,
[string]$State
)
# Check required fields
if ([String]::IsNullOrEmpty($FirstName) -or [String]::IsNullOrEmpty($LastName)) {
Write-Error "Customer name is required"
return $false
}
# Check email format if provided
if (-not [String]::IsNullOrEmpty($Email)) {
if ($Email -notmatch "^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$") {
Write-Error "Invalid email format"
return $false
}
} else {
Write-Warning "No email provided for customer $FirstName $LastName"
}
# Validate US state if provided
if (-not [String]::IsNullOrWhiteSpace($State)) {
$validStates = @('AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY')
if ($validStates -notcontains $State.ToUpper()) {
Write-Error "Invalid US state code: $State"
return $false
}
}
# All validations passed
Write-Host "Processing customer: $FirstName $LastName" -ForegroundColor Green
return $true
}
# Test the function with different data
Process-CustomerData -FirstName "John" -LastName "Doe" -Email "john.doe@example.com" -State "NY"
Process-CustomerData -FirstName "" -LastName "Smith" -Email "jane@example.com" -State "CA"
Process-CustomerData -FirstName "Bob" -LastName "Johnson" -Email "invalid-email" -State "ZZ"
This example demonstrates how checking for null or empty values helps create a robust data validation routine.
Let me show you another simple example.
Let’s say you have a script that processes user input. You want to ensure that the input is not $null or empty before proceeding. Here’s how you might write that:
$userInput = Read-Host "Please enter your name"
if ([string]::IsNullOrWhiteSpace($userInput)) {
Write-Host "You did not enter a name!"
} else {
Write-Host "Hello, $userInput!"
}
This script prompts the user for their name and checks if the input is $null, empty, or just whitespace. It only greets the user if they’ve entered a valid name.
Check out Rename Folders to Uppercase or Lowercase Using PowerShell
Best Practices for Handling Null or Empty Variables in PowerShell
Based on my experience working with PowerShell in enterprise environments, here are some best practices:
- Always check inputs before processing them: This prevents unexpected errors and makes your scripts more robust.
- Be consistent in your approach: Pick one method and use it consistently throughout your codebase.
- Use the appropriate method for each data type: Different data types (strings, arrays, objects) might require different null-checking techniques.
- Consider using parameter validation: When defining functions, use PowerShell’s built-in parameter validation attributes:
function Do-Something {
param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$RequiredParam
)
# Your code here
}
- Use default values where appropriate:
function Get-UserInfo {
param (
[string]$Username = $env:USERNAME
)
if ([String]::IsNullOrEmpty($Username)) {
$Username = "DefaultUser"
}
# Process with guaranteed non-empty username
}
In this tutorial, I have explained how to check if a variable is null or empty in PowerShell using several methods.
I hope you found this article helpful. If you have any questions or want to share your own methods for handling null or empty variables in PowerShell, please leave a comment below.
You may also like:
- Replace Carriage Returns in Strings Using PowerShell
- How to Split a String by Length in PowerShell?
- PowerShell Substring() Example

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.