Eliminating Empty Text in PowerShell

by Sep 5, 2012

If you wanted to exclude results with empty (text) columns, you can filter based on $null values. This will get you all processes with a valid company information:

Get-Process | 
    Where-Object { $_.Company -ne $null } | 
  Select-Object Name, Company, Description

Or not. Sometimes, text can also be an empty string, so you would have to check for that as well:

Get-Process | 
  Where-Object { $_.Company -ne $null } | 
  Where-Object { $_.Company -ne '' } | 
  Select-Object Name, Company, Description

To combine both checks, use a .NET method which checks for null or empty text:

Get-Process | 
Where-Object { -not [string]::IsNullorEmpty($_.Company) } | 
Select-Object Name, Company, Description

Twitter This Tip! ReTweet this Tip!