PowerShell

PowerShell: How to find empty Attributes

We are often looking for attributes, but what about the empty attributes? How can I find out if an attribute is empty? That is the focus of this article. I will show a practical example of how to find these empty attributes.

Let’s start with how to find attributes which are NOT empty. The following code retrieves all svchost process Attributes which have a value.

$process = Get-Process -Name "svchost" | Select-Object -First 1
$process.PSObject.Properties | Where-Object { $null -eq $_.Value } | Select-Object Name, Value

Looking for Empty Attributes

Now we are looking for empty attributes.

$process.PSObject.Properties | Where-Object { $null -ne $_.Value } | Select-Object Name, Value

As you can see, in the first example we have called attributes that are not empty and in the second example we have called attributes that are empty.

A more practical example is the search for empty attributes for Active Directory users.

Here is an example:

# Which users have empty streetaddress?
$property = 'streetaddress'
Get-ADUser -Filter * -Properties $property | Where-Object { $null -eq $_.$property } | 
Select-Object Name, $property

4 replies »

  1. Hi Patrick, very useful post, thanks!

    Notice there is a typo. First command says -eq where should be -ne, and the second says -ne where should be -eq.

    I like your posts.

    Joan

    Like

Leave a reply to Joan Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.