Get ADUser question

This topic contains 5 replies, has 5 voices, and was last updated by  Don Weber 1 year ago.

  • Author
    Posts
  • #47649

    Don Weber
    Participant

    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

  • #47667

    Craig Duff
    Participant

    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 1 year ago by  Craig Duff.
    • This reply was modified 1 year ago by  Craig Duff.
  • #47679

    TomBaker
    Participant

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

  • #47704

    Mark Prior
    Participant

    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 1 year ago by  Mark Prior.
  • #47739

    Dan Potter
    Participant

    ANR would be more accurate and save you time.

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

  • #47832

    Don Weber
    Participant

    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

You must be logged in to reply to this topic.