Filter Empty Values Using PowerShell Where-Object Cmdlet

When working with data in PowerShell, it’s common to encounter empty values, such as empty strings, null values, or empty arrays. These can interfere with data processing and may need to be filtered out. PowerShell provides a powerful cmdlet, Where-Object, which can be used to filter objects based on their property values. In this PowerShell tutorial, we will discuss how to use Where-Object to filter out empty values with examples.

To filter query results in PowerShell for empty or null values, use the Where-Object cmdlet with conditions such as { $_ -ne "" } for strings, { $_.PropertyName -ne "" } for object properties, { $_.Count -gt 0 } for arrays, and { $_ -ne $null -and $_ -ne "" } to exclude both null and empty strings. This allows you to precisely refine your data, ensuring that only non-empty, meaningful values are processed in your scripts.

PowerShell Where-Object cmdlet allows you to specify a script block that evaluates to $true or $false for each object passed through the pipeline. If the script block evaluates to $true, the object is passed along the pipeline; otherwise, it is discarded.

PowerShell Where-Object Empty String

Let’s start with the simplest case: filtering out empty strings in PowerShell using Where-Object Cmdlet. An empty string in PowerShell is a string object that contains no characters. Here’s an example of how to filter out empty strings from an array of strings in PowerShell:

# Create an array of strings with some empty values
$stringArray = "New York", "", "Phoenix", "", "!"

# Filter out the empty strings
$nonEmptyStringArray = $stringArray | Where-Object { $_ -ne "" }

# Output the non-empty strings
$nonEmptyStringArray

In this example, $_ represents each string in the array as it comes through the pipeline. The -ne operator is used to check if the string is not equal to an empty string ("") in PowerShell.

After executing the code, you can see the output in the screenshot below:

powershell where-object empty

PowerShell Where-Object Property Empty

Sometimes, you might work with objects that have properties, and you need to filter out objects where a specific property is empty. Here’s how you can do that:

# Create an array of custom objects
$objectArray = @(
    [PSCustomObject]@{ Name = "Alice"; Age = 30 },
    [PSCustomObject]@{ Name = ""; Age = 25 },
    [PSCustomObject]@{ Name = "Bob"; Age = $null }
)

# Filter out objects with an empty 'Name' property
$objectsWithName = $objectArray | Where-Object { $_.Name -ne "" }

# Output the objects that have a 'Name'
$objectsWithName

In the above code, we’re filtering out objects where the Name property is an empty string.

You can see the output in the screenshot below:

PowerShell Where-Object Property Empty

PowerShell Where-Object Empty Array

Filtering out empty arrays requires a slightly different approach. An empty array in PowerShell is an array with a count of zero. Here’s an example:

# Create an array of arrays with some empty arrays
$arrayOfArrays = @(1, 2, 3), @(), @(4, 5), @()

# Filter out the empty arrays
$nonEmptyArrays = $arrayOfArrays | Where-Object { $_.Count -gt 0 }

# Output the non-empty arrays
$nonEmptyArrays

In this case, we use the Count property of the array to determine if it’s empty or not in PowerShell.

You can see the output in the screenshot below:

PowerShell Where-Object Empty Array

PowerShell Filter Null or Empty Using Where-Object

Null values can also be filtered using Where-Object. Null in PowerShell is represented by $null. To filter out null or empty values in PowerShell, you can combine checks for null and empty strings. Here’s how:

# Create an array with null and empty values
$mixedValues = $null, "", "PowerShell", $null, "Scripting"

# Filter out null and empty values
$validValues = $mixedValues | Where-Object { $_ -ne $null -and $_ -ne "" }

# Output the valid values
$validValues

This script will return an array containing only “PowerShell” and “Scripting”.

You can see the output after I executed the script using VS code.

PowerShell Filter Null or Empty Using Where-Object

Conclusion

Using the Where-Object cmdlet in PowerShell, you can filter out empty strings, objects with empty properties, empty arrays, and null or empty values.

In this PowerShell tutorial, I have explained everything about “PowerShell where-object empty” with various examples:

  • PowerShell where object empty string
  • PowerShell where-object property empty
  • PowerShell where-object empty array
  • PowerShell filter null or empty using where-object

You may also like: