Author Posts

July 20, 2016 at 8:35 pm

Hello,

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,
Don

July 20, 2016 at 10:06 pm

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.

  • This reply was modified 2 years ago by  Craig Duff.
  • This reply was modified 2 years ago by  Craig Duff.

July 21, 2016 at 2:54 am

$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.

July 21, 2016 at 8:07 am

same as above really just arranged differently

"test.csv" just contains email address's
example1@contoso.com
example2@contoso.com

##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

  • This reply was modified 2 years ago by  Mark Prior.

July 21, 2016 at 11:29 am

ANR would be more accurate and save you time.

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

July 21, 2016 at 7:24 pm

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. 🙂

Don