Get ADUser question


If I have a list of email addresses (which are mail attributes for our AD users). Is there a way to use Get ADUser to get these accounts with these values as the mail attribute? What I am wanting to do is to get a list of SAM account names that correlate to the email addresses.

Thank you,

You can use Get-ADUser’s Filter parameter to do queries like that against just about any attribute.

Get-ADUser -Filter 'mail -eq $email'

Now there are several syntaxes for filter. My example requires the use of single quotes (or braces) and a simple variable to hold the email address. It can have different syntaxes. If you used double quotes, then you’d have to quote around $email too.

$emails = @(‘addr1’,‘addr2’)

$ldapfilter = ‘(|{0})’ -f (($emails|foreach-object {“(mail=$_)”}) -join ‘’ )
Get-aduser -ldapfilter $ldapfilter -properties mail

If you are like me and prefer to use ldapfilter the above constructs an “or” filter in ldapfilter syntax so you only call Get-aduser once rather than once per address.

If you need to know which addresses did not match then compare the results mail property with your original list.

same as above really just arranged differently

“test.csv” just contains email address’s

##Get Data##

$mails = get-content "c:\test.csv"
$results = foreach ($mail in $mails)

##Find what we want##
get-aduser -Properties mail -LdapFilter "(mail=$mail)" | Select-Object samaccountname, mail

##Do something with result##

$results | Out-GridView

ANR would be more accurate and save you time.

get-aduser -filter {anr -eq $email}

Thank you so much for all your great ideas! I knew it could be done. I’m just not up to speed yet. I’m still at the Hello World level. :slight_smile: