PowerShell Where-Object for Null or Empty Values

To find null or empty values with Where-Object, test the property with [string]::IsNullOrWhiteSpace(). It catches $null, empty strings and values that are only spaces. Here it finds employees with no department in a CSV file:

Import-Csv -Path C:\psfaqs\employees.csv |
    Where-Object { [string]::IsNullOrWhiteSpace($_.Department) } |
    Select-Object -Property Name, Department

Output:

Name       Department
----       ----------
Bob Smith
Dan Miller
PowerShell Where-Object finds null or empty values in a CSV column
Rows with a blank department (PowerShell 7)

Dan’s department is two spaces, so it looks filled in but isn’t. To keep only the rows that have a value, add -not:

Import-Csv -Path C:\psfaqs\employees.csv |
    Where-Object { -not [string]::IsNullOrWhiteSpace($_.Department) } |
    Select-Object -Property Name, Department

Output:

Name          Department
----          ----------
Alice Johnson Sales
Carol Davis   Marketing

I ran every example in PowerShell 7.6 and Windows PowerShell 5.1. One shortcut works only in PowerShell 7, and I point it out below.

Why doesn’t -eq $null find blank CSV cells?

Import-Csv turns a blank cell into an empty string, not $null. So a -eq $null filter finds nothing:

$rows = Import-Csv -Path C:\psfaqs\employees.csv

"Bob's department is null:  $($null -eq $rows[1].Department)"
"Bob's department is empty: $($rows[1].Department -eq '')"
"Rows found with -eq `$null: $(@($rows | Where-Object { $_.Department -eq $null }).Count)"

Output:

Bob's department is null:  False
Bob's department is empty: True
Rows found with -eq $null: 0

The same goes for most text you read from files. That’s why I reach for IsNullOrEmpty or IsNullOrWhiteSpace rather than comparing with $null.

Null vs empty vs whitespace: which check to use

Each check catches a different set of values. Here are four common ones run against the same objects:

$items = @(
    [pscustomobject]@{ Name = 'Null';       Dept = $null }
    [pscustomobject]@{ Name = 'Empty';      Dept = '' }
    [pscustomobject]@{ Name = 'Spaces';     Dept = '   ' }
    [pscustomobject]@{ Name = 'Has value';  Dept = 'IT' }
)

$tests = [ordered]@{
    '$null -eq $_.Dept'          = { $null -eq $_.Dept }
    '-not $_.Dept'               = { -not $_.Dept }
    'IsNullOrEmpty($_.Dept)'     = { [string]::IsNullOrEmpty($_.Dept) }
    'IsNullOrWhiteSpace($_.Dept)' = { [string]::IsNullOrWhiteSpace($_.Dept) }
}

foreach ($test in $tests.GetEnumerator()) {
    $found = ($items | Where-Object -FilterScript $test.Value).Name -join ', '
    '{0,-29} {1}' -f $test.Key, $found
}

Output:

$null -eq $_.Dept             Null
-not $_.Dept                  Null, Empty
IsNullOrEmpty($_.Dept)        Null, Empty
IsNullOrWhiteSpace($_.Dept)   Null, Empty, Spaces
PowerShell Where-Object null, empty and whitespace checks compared
Four null and empty checks side by side (Windows PowerShell 5.1)
CheckCatchesUse it when
$null -eq $_.PropOnly $nullThe property is missing or never set
-not $_.Prop$null, empty string, 0, $falseYou want a quick check on text
[string]::IsNullOrEmpty()$null and empty stringSpaces count as a real value
[string]::IsNullOrWhiteSpace()$null, empty and spaces onlyUser input, CSV and form data

More detail on the two .NET methods is in IsNullOrEmpty vs IsNullOrWhiteSpace.

Careful with numbers: 0 is not empty

A plain truthy check like Where-Object { $_.Stock } treats 0 the same as $null. That quietly drops real data:

$products = @(
    [pscustomobject]@{ Name = 'Mouse';    Stock = 12 }
    [pscustomobject]@{ Name = 'Keyboard'; Stock = 0 }
    [pscustomobject]@{ Name = 'Monitor';  Stock = $null }
)

$truthy  = ($products | Where-Object { $_.Stock }).Name -join ', '
$notNull = ($products | Where-Object { $null -ne $_.Stock }).Name -join ', '

"Truthy check: $truthy"
"Null check:   $notNull"

Output:

Truthy check: Mouse
Null check:   Mouse, Keyboard
PowerShell Where-Object truthy check drops items with a value of zero
The truthy check drops the keyboard with 0 in stock (PowerShell 7)

The keyboard has 0 in stock, which is a real value. For numbers and dates, compare with $null instead.

Put $null on the left side

When the value on the left is an array, -ne $null filters the array instead of returning True or False:

$tags = @('sales', $null, 'west')

"`$tags -ne `$null returns: $(($tags -ne $null) -join ', ')"
"`$null -ne `$tags returns: $($null -ne $tags)"

Output:

$tags -ne $null returns: sales, west
$null -ne $tags returns: True

With $null on the left, you always get a single True or False. Microsoft explains this in about_Comparison_Operators.

Short syntax for non-empty properties

Where-Object also takes a property name on its own. It keeps objects where that property has a truthy value:

$rows = Import-Csv -Path C:\psfaqs\employees.csv
($rows | Where-Object Phone).Name

Output:

Alice Johnson
Bob Smith
Dan Miller

PowerShell 7 also has -Not for the opposite, which returns objects where the property is empty:

$rows = Import-Csv -Path C:\psfaqs\employees.csv
($rows | Where-Object -Not Phone).Name

Output in PowerShell 7:

Carol Davis

Windows PowerShell 5.1 doesn’t have -Not. It stops with an error that says the parameter name ‘Not’ is ambiguous, so use a script block there. The Where-Object documentation lists both syntaxes.

Filter out empty arrays

For arrays, check Count. An empty array has a count of 0:

$batches = @(1, 2, 3), @(), @(4, 5), @()

$full = $batches | Where-Object { $_.Count -gt 0 }
"Batches: $($batches.Count), not empty: $($full.Count)"

Output:

Batches: 4, not empty: 2

To check a single array, see how to check if an array is empty.

Find empty files and folders

Empty works for files too. An empty file has a Length of 0, and an empty folder has nothing inside it:

Get-ChildItem -Path C:\psfaqs\Uploads -File |
    Where-Object Length -EQ 0 |
    Select-Object -ExpandProperty Name

Get-ChildItem -Path C:\psfaqs\Uploads -Directory |
    Where-Object { -not (Get-ChildItem -LiteralPath $_.FullName -Force) } |
    Select-Object -ExpandProperty Name

Output:

invoice-1043.txt
2026-10

-Force includes hidden files, so a folder with only a hidden file doesn’t count as empty.

Frequently Asked Questions

How do I filter out null or empty values with Where-Object?

Use Where-Object { -not [string]::IsNullOrWhiteSpace($_.Name) }. It skips $null, empty strings and values that are only spaces.

Why doesn’t Where-Object { $_.Prop -eq $null } find empty values?

Empty strings aren’t $null. Blank cells from Import-Csv are empty strings, so use IsNullOrEmpty or IsNullOrWhiteSpace.

How do I find objects where a property is not null?

Use Where-Object { $null -ne $_.Prop }. Put $null on the left so array values don’t change the result.

Does Where-Object -Not work in PowerShell 5.1?

No. -Not is only in PowerShell 7. In 5.1, use Where-Object { -not $_.Prop }.

Does -not $_.Prop treat 0 as empty?

Yes. 0 and $false count as false, so those objects are dropped too. Compare with $null for numbers.

If you work with Where-Object a lot, these guides go further: