Removing Empty Things

by Jun 3, 2010

How do you exclude objects based on empty properties? For example, WMI returns all kinds of "network adapters:"

Get-WMIObject Win32_NetworkAdapterConfiguration

To focus only on those that have an IPAddress assigned, you should exclude any object that has an empty IPAddress property like this:

Get-WMIObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress }

That's all. Where-Object will automatically convert any null-property to $false and any other to $true. That's why this line will only display processes that have a non-null company property:

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

Twitter This Tip!
ReTweet this Tip!