PowerShell Where-Object for Null or Empty Values

In PowerShell, you might have to deal with null or empty values. In this PowerShell tutorial, I will explain, how to use Where-Object to work with null and empty values in PowerShell.

To filter objects in PowerShell where a property is null or empty, you can use the Where-Object cmdlet with a script block that checks for these conditions. For example, to find all items where the “Name” property is either null or an empty string, you would use $items | Where-Object { -not $_.Name }. To specifically check for non-null and non-empty strings, use $items | Where-Object { $_.Name } or $items | Where-Object { -not [string]::IsNullOrEmpty($_.Name) } for more clarity.

The Where-Object cmdlet in PowerShell allows you to filter objects based on their property values.

PowerShell where-object null

To check for null values using Where-Object in PowerShell, you can use the -eq $null operator. Here’s a simple example:

$users = Get-User -Filter *
$nullUsers = $users | Where-Object { $_.Property -eq $null }

In this example, $nullUsers will contain all users where the specified property is null.

PowerShell where-object not null checking

Conversely, if you want to filter objects that have a non-null property, you can use -ne $null in PowerShell with the Where-Object. Here’s how you would do that:

$nonNullUsers = $users | Where-Object { $_.Property -ne $null }

This will return all users where the Property is not null.

PowerShell where-object not null or empty Checking

Sometimes, a property may not be null, but it could be an empty string, which is often just as unusable as a null value. To check for values that are neither null nor empty in PowerShell, you can combine -ne $null with another condition to exclude empty strings:

$validUsers = $users | Where-Object { $_.Property -ne $null -and $_.Property -ne '' }

This ensures that Property is neither null nor an empty string.

Checking for Strings That Are Not Null or Empty

When dealing with strings, you might want to ensure that a property is both not null and not an empty string. PowerShell provides a method called String.IsNullOrEmpty() which can be used to check for this:

$validUsers = $users | Where-Object { -not [string]::IsNullOrEmpty($_.Property) }

This will return all users where the Property is a non-null and non-empty string.

Here is an example:

Let’s put these concepts into practice with some examples. Suppose you have a list of processes, and you want to find those that don’t have a company name associated with them:

$processes = Get-Process
$processesWithoutCompany = $processes | Where-Object { $_.Company -eq $null }

This will give you all processes where the Company property is null.

If you want to find processes with a company name that is not null or empty, you can do the following:

$processesWithValidCompany = $processes | Where-Object { $_.Company -ne $null -and $_.Company -ne '' }

And if you want to find processes where the company name is specifically a non-empty string, you can use:

$processesWithValidCompany = $processes | Where-Object { -not [string]::IsNullOrEmpty($_.Company) }

Conclusion

Understanding how to check for null, not null, and empty values in PowerShell is crucial for scripting and automation tasks. The Where-Object cmdlet can help you filter objects based on null or empty. Remember to use -eq $null to check for null values, -ne $null for not null values, and consider using [string]::IsNullOrEmpty() when dealing with strings to check for non-null and non-empty values.

In this PowerShell tutorial, we discuss how to check for null or empty in PowerShell where-object.

You may also like: