Get-aduser -Filter email address

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?

June 25th, 2014 2:40pm

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

}
Free Windows Admin Tool Kit Click here and download it now
June 25th, 2014 2:48pm

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}

June 25th, 2014 2:56pm

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?

Free Windows Admin Tool Kit Click here and download it now
June 25th, 2014 3:00pm


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

June 25th, 2014 3:08pm

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

Free Windows Admin Tool Kit Click here and download it now
June 25th, 2014 3:15pm

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.
June 25th, 2014 3:22pm

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}


Free Windows Admin Tool Kit Click here and download it now
June 25th, 2014 3:27pm

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

June 25th, 2014 3:29pm

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?

Free Windows Admin Tool Kit Click here and download it now
June 25th, 2014 3:32pm

This works, and the only difference i see is that you placed the $_.email attribute in a new object.

 

June 25th, 2014 3:46pm

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.

Free Windows Admin Tool Kit Click here and download it now
June 25th, 2014 3:51pm

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. 
June 25th, 2014 6:22pm

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.

Free Windows Admin Tool Kit Click here and download it now
March 23rd, 2015 6:44pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics