locked
Get-aduser -Filter email address RRS feed

  • Question

  • Hi, I have a csv file with a email address and the employ ID. I which to update the emply id according to the email address

    csv:

    Email;employerID;FirstName;LastName;

    a.a@test.com;123456789;a;a;

    b.b@test.com;789456123;b;b;

    I tried querying the user according to email but the user is not found:

    $test = Import-csv -Path \\tsclient\c\temp\test.csv -delimiter ";"
    Foreach ($u in $test) {Get-aduser -Filter { emailaddress -Like $u.email} -Properties emailaddress}
    
    


    Which i find strange, because when i run following commandlet the correct address are displayed.

    $test = Import-csv -Path \\tsclient\c\temp\test.csv -delimiter ";"
    Foreach ($u in $test) {write-host $u.email}
    

    I even tried, but same result:

    $test = Import-csv -Path \\tsclient\c\temp\test.csv -delimiter ";"
    Foreach ($u in $test) {Get-aduser -Filter { emailaddress -Like $($u.email)} -Properties emailaddress}

    What am i doing wrong?


    Answers provided are coming from personal experience, and come with no warranty of success. I as everybody else do make mistakes.

    Wednesday, June 25, 2014 2:40 PM

Answers

All replies

  • Hi,

    The -like test uses wildcards, but you're not using any. Try this instead to test for exact matches:

    Import-csv -Path \\tsclient\c\temp\test.csv -delimiter ";" | ForEach {
    
        Get-ADUser -Filter "EmailAddress -eq '$($_.email)'" -Properties EmailAddress
    
    }


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Wednesday, June 25, 2014 2:48 PM
  • The attribute in AD is mail, note emailaddress.

    Try this:

    $test = Import-csv -Path \\tsclient\c\temp\test.csv -delimiter ";"
    Foreach ($u in $test) {Get-aduser -Filter { mail -Like $u.email} -Properties mail}

    Wednesday, June 25, 2014 2:56 PM
  • Sorry, still no go:

    Get-ADUser : The search filter cannot be recognizedAt line:2 char:5+     Get-ADUser -Filter "EmailAddress -eq '$($_.email)'" -Properties EmailAddress+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException    + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    I used the quotes in previous cmdlets for filters as you did, however if you check documentation, you see they use {} for the filter. Is there a difference?


    Answers provided are coming from personal experience, and come with no warranty of success. I as everybody else do make mistakes.

    Wednesday, June 25, 2014 3:00 PM

  • I used the quotes in previous cmdlets for filters as you did, however if you check documentation, you see they use {} for the filter. Is there a difference?

    Yes, the documentation examples are wrong. The -Filter parameter specifically states that it's looking for a string. The scriptblock will still work, but not in every case.

    The blurb I posted does work for me. As a test, try this:


    Get-ADUser -Filter "EmailAddress -eq 'a.a@test.com'" -Properties EmailAddress


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Wednesday, June 25, 2014 3:08 PM
  • The attribute in AD is mail, note emailaddress.

    Try this:

    $test = Import-csv -Path \\tsclient\c\temp\test.csv -delimiter ";"
    Foreach ($u in $test) {Get-aduser -Filter { mail -Like $u.email} -Properties mail}

    Still no go, 

    Get-aduser : Property: 'email' not found in object of type: 'System.Management.Automation.PSCustomObject'.
    At line:1 char:24
    + Foreach ($u in $test) {Get-aduser -Filter { mail -Like $u.email} -Properties mai ...
    +                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Comm
       ands.GetADUser
    

    However, pure out of interest.

    How come that following does give a valid result:

    Get-aduser -Filter { emailaddress -like "test@test.com"} -Properties Emailaddress -server emea

    Because both attributes exist:

    PS C:\Windows\system32\WindowsPowerShell\v1.0> Get-aduser test -server emea -Properties * | fl Emailaddress, mail
    
    
    Emailaddress : test@test.com
    mail         : test@test.com


    Answers provided are coming from personal experience, and come with no warranty of success. I as everybody else do make mistakes.

    Wednesday, June 25, 2014 3:15 PM
  • Because both attributes exist:
    EmailAddress is PowerShell's friendly representation of the LDAP mail attribute. It's like how PowerShell has a PasswordLastSet property that is a human readable version of pwdLastSet.

    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Wednesday, June 25, 2014 3:22 PM
  • Get-aduser : Property: 'email' not found in object of type: 'System.Management.Automation.PSCustomObject'.

    This implies that the object $u does not have a property named email.

    $test = Import-csv -Path \\tsclient\c\temp\test.csv -delimiter ";"
    Foreach ($u in $test) {$u.email}


    Wednesday, June 25, 2014 3:27 PM
  • Yes that does work.

    And i do not think that the csv is the problem because when i Look at the attribute, it gives a valid email address.

    Import-csv -Path "\\tsclient\c\temp\test.csv" -delimiter ";" | ForEach {
    
    write-host $_.email
    }
    
    test@test.com
    test1@test.com
    test2@test.com
    test3@test.com


    Answers provided are coming from personal experience, and come with no warranty of success. I as everybody else do make mistakes.

    Wednesday, June 25, 2014 3:29 PM
  • Just in case, try this:

    Import-csv -Path \\tsclient\c\temp\test.csv -delimiter ";" | ForEach {
    
        $mailAddr = $_.email
    
        Write-Host "About to process $mailAddr"
    
        Get-ADUser -Filter "EmailAddress -eq '$mailAddr'" -Properties EmailAddress
    
    }

    Little extra output this time, does this show anything suspect?


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    • Proposed as answer by atom_acres Wednesday, June 25, 2014 6:22 PM
    Wednesday, June 25, 2014 3:32 PM
  • This works, and the only difference i see is that you placed the $_.email attribute in a new object.

     


    Answers provided are coming from personal experience, and come with no warranty of success. I as everybody else do make mistakes.

    Wednesday, June 25, 2014 3:46 PM
  • That's exceedingly strange. I only used a new variable because I was being lazy and didn't want to type as much.

    That is the only difference between what I posted the first time.


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    • Marked as answer by Killerbe Thursday, June 26, 2014 7:24 AM
    Wednesday, June 25, 2014 3:51 PM
  • I have found it is best to do it as Mike suggested when using foreach. I have had several scripts where the results were not as expected but using something like $var1 = $_.name then using the variable ($var1) instead of $_.name would work great but not the other way around. I was going to suggest this but Mike beat me to it. Glad to see it worked. 
    Wednesday, June 25, 2014 6:22 PM
  • I think the issue is actually with Get-AdUser and not Foreach.  I recently had this same problem with Get-AdUser and wasn't in a Foreach block at all.  I was just doing: Get-AdUser -Filter { Name -eq $MyObject.MyPropertyName } and $MyObject.MyPropertyName definitely had a value.  Like Mike, I tried all the various quoting combos without any luck.  

    I finally gave up and just assigned a variable to the property value too, but I can't say I like that fix very much. You should be able to dot-walk to property values in a script block. If you couldn't, your code would have tons of unnecessary variable assignments. I don't like creating a variable to use a single time when I could just use a property instead.  Seems like either a bug with Get-AdUser, or odd behavior to me.

    Monday, March 23, 2015 10:41 PM
  • I know it's an old one but since I didnt find an answer...

    I agree there's a bug in Get-ADUser as I couldn't parse a property of an object:

    E.G: Get-ADUser -filter "emailaddress -eq '$('$user.emailaddress')'" -properties * | select <whatever you want>

    didn't work

    Get-ADUser -filter "emailaddress -eq '$user.emailaddress'" -properties * | select <whatever you want>

    didnt work either

    at the end I had to inefficiently transfer the content to a simple variable and finally made it work using:

    Get-ADUser -filter "emailaddress -eq '$var'" -properties * | select <whatever you want>

    Wednesday, February 8, 2017 1:20 PM