Filtering for $null Values with Get-ADUser
Get-ADUser includes a -Filter parameter that lets you define queries for users with specific characteristics. Today I was trying to figure out how to filter for $null values. Here is my example of why you might care to do this...... Let's say that in your organization, that you always set the Department attribute to match the department that users work in. This could be required for dynamic groups or address books. You've just created 100 new users, but forgot to configure the department. You need to make a query for all of the users without a department configured. My first attempt was this: Get-ADUser -Filter {company -eq $null} However, this generates an error. You can't use $null in a filter. What finally worked was this: Get-ADUser -Filter {company -notlike "*"} The gets a list of users where the company attribute is not like anything. I should also note that if you try to query for not equal (-ne) then it will skip $null values when comparing. The above exa...