Use $null to Identify Empty Data

by May 3, 2010

You can use $null as a special variable representing "nothing." You can use it to identify (and sort out) non-existing data like so:

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

If you still get processes with an empty company column, then these do return a company's information but it is an empty string. If you want to also eliminate these empty strings, try adding this:

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

Twitter This Tip! ReTweet this Tip!