Removing Empty Things

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

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!


Posted Jun 03 2010, 08:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.