How can I check if a string is null or empty in PowerShell?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Use [string]::IsNullOrEmpty($value) to check whether a string is $null or "" in PowerShell. If whitespace-only strings should also count as empty, use [string]::IsNullOrWhiteSpace($value) instead. Both are .NET static methods available in every PowerShell version since 2.0, and they are more reliable than trusting PowerShell's implicit boolean coercion.
IsNullOrEmpty: The Standard Check
[string]::IsNullOrEmpty() returns $true when the input is either $null or a zero-length string. This is the right choice when whitespace characters are considered valid content.
This method maps directly to the .NET String.IsNullOrEmpty method, so its behavior is identical to what C# developers expect. It does not consider " " (whitespace) as empty.
IsNullOrWhiteSpace: When Blanks Should Be Rejected
In most input validation scenarios, a string containing only spaces, tabs, or newlines should be treated the same as an empty string. [string]::IsNullOrWhiteSpace() covers all three cases.
This is typically the better default for configuration files, user input, and command-line arguments where visually blank input is meaningless.
Direct $null Comparison
Sometimes you need to distinguish between a variable that was never assigned ($null) and one that was explicitly set to an empty string. In that case, compare directly.
Always place $null on the left side of the -eq operator. PowerShell's comparison operators behave differently when a collection appears on the left side, and placing $null first avoids that ambiguity.
Why You Should Avoid -not $value
PowerShell allows boolean coercion with if (-not $value), and it evaluates to $true for both $null and "". But this approach is fragile.
The problem is that -not also evaluates to $true for the integer 0, an empty array, and other falsy values. If the variable type changes during refactoring, this check silently changes meaning.
For string validation, explicit string methods communicate intent and prevent category errors.
Comparison Table
| Method | Catches $null | Catches "" | Catches " " | Type-Safe | Recommended For |
[string]::IsNullOrEmpty() | Yes | Yes | No | Yes | Strict null/empty checks |
[string]::IsNullOrWhiteSpace() | Yes | Yes | Yes | Yes | Input validation |
$null -eq $value | Yes | No | No | Yes | Distinguishing null from empty |
-not $value | Yes | Yes | No | No | Quick scripts only |
$value.Length -eq 0 | No (throws) | Yes | No | Partial | Avoid (null-unsafe) |
Parameter Validation Attributes
PowerShell functions support validation attributes that reject bad input at the boundary, before your logic runs. This is often better than scattering null checks throughout the function body.
For stricter validation that also rejects whitespace:
Centralizing Checks with a Helper Function
When the same validation pattern repeats across a script, extract it into a helper to keep behavior consistent.
Note the [AllowNull()] and [AllowEmptyString()] attributes. Without them, PowerShell's parameter binding coerces $null to "" before the function body runs, which would change the behavior.
Practical Example: Config File Validation
Here is a real-world pattern that combines these techniques to validate a configuration hashtable:
Common Pitfalls
- Using
-not $valuefor string validation when the variable might later hold a non-string type. The check silently changes meaning. - Choosing
IsNullOrEmptywhen whitespace-only input should also be rejected. Users who paste spaces into a form field will bypass the check. - Placing
$valueon the left side of-eq $null. If$valueis an array, PowerShell filters the array instead of comparing it. - Checking
$value.Length -eq 0without first checking for$null. This throws aNullReferenceExceptionequivalent error. - Forgetting
[AllowNull()]on function parameters. PowerShell coerces$nullto""for[string]parameters by default, which masks true null values. - Repeating slightly different validation conditions throughout a script instead of using a shared helper or parameter validation attribute.
Summary
[string]::IsNullOrEmpty()is the standard check for null or zero-length strings.[string]::IsNullOrWhiteSpace()is the better default for input validation because it also catches whitespace-only strings.- Use direct
$null -eq $valuecomparison only when you must distinguish null from empty. - Avoid
-not $valuefor string validation. It works by accident and breaks when the type changes. - Use
[ValidateNotNullOrEmpty()]on function parameters to catch bad input at the boundary. - Centralize repeated checks in a helper function to keep validation consistent across scripts.

