PowerShell Tip: Quickly Find Out Active Directory Users with Missing Email Address

Going through AD trying to find the users that don’t have email address associated is almost impossible, especially when your AD contains hundreds of user accounts. But with PowerShell, you can do it and find all users that don’t have email address within seconds.

Here is how you can achieve it.

First, open Windows PowerShell window. If you are on Windows 8.1, you can simply press Win+X key and select Windows PowerShell from the list. Or, you can press Win key, type “powershell”, and launch it from the search result list.

Then, run the following cmdlet first to search the AD and save the result in $search variable. If it runs ok, you won’t see anything in return.

$search = [ADSISearcher]"(&(sAMAccountType=$(0x30000000))(!(mail=*)))"

And run the following cmdlet basically to show the result.

$searcher.FindAll() | ForEach-Object { $_.GetDirectoryEntry() } | Select-Object -Property sAMAccountName, name, mail

PowerShell - AD user with no email

If you want to list all users with email address, change the first cmdlet to something like below:

$search = [ADSISearcher]"(&(sAMAccountType=$(0x30000000))(mail=*))"

If you want to search for a list of users with roaming profile enabled, use something like this:

$search = [ADSISearcher]"(&(sAMAccountType=$(0x30000000))(profilepath=*))"

And add the field name ProfilePath to the second line to show what the roaming profile path is.

You actually can pretty much search for anything with this PowerShell cmdlet. What you need to figure out is just the field name that presents the field you are searching for.

Try it yourself in an Active Directory environment, and you will be amazed.

Kent Chen

Microsoft MVP, IT Professional, Developer, Geek, and the co-founder of Next of Windows.

Last updated: 08/04/2014

Posted in: Tips & Tricks
Discover more: ,