Hans Torres
Hans Torres

Reputation: 81

get-aduser using emailaddress

when i want to get some information from an user i use this:

Get-ADUser -Filter {EmailAddress -eq '[email protected]'}

but when i wanna check the information from a bulk of users i try this:

$batch| foreach {Get-ADUser -Filter {emailaddress -eq $_.email}} 

email is the name of the variable in the CSV file but i am getting this error:

"Get-ADUser : Property: 'email' not found in object of type: 'System.Management.Automation.PSCustomObject'"

i can not use the identity because te emailaddess is not supported for this one

Upvotes: 8

Views: 79082

Answers (4)

Andrew Maiman
Andrew Maiman

Reputation: 111

Another quick solution is to only pass the property that you want to filter on to the filter expression (this works well when working with CSV imports). Using your example it would change to:

$batch.email| foreach {Get-ADUser -Filter {emailaddress -eq $_}} 

Upvotes: 0

Stanislav Baran
Stanislav Baran

Reputation: 1

Or you can do it per invoke-expression.

$content = Get-Content c:\folder\file.txt

foreach ($emails in $content)

{

    $command = "get-aduser  -Filter {emailaddress -eq ""$emails""} | select -ExpandProperty SamAccountName"

    Invoke-Expression $command

}

Works too :)

Upvotes: 0

Carlos del Rio
Carlos del Rio

Reputation: 23

What kind of format are you getting this information in?

Personally, I like to make a temporary file then query using a variable in the for loop. For instance, if I had a file that was a list of email addresses at C:\Users\MyUser\Documents\emailList.txt I would do the following:

$my_list = Get-Content C:\Users\MyUser\Documents\emailList.txt
foreach ($x in $my_list){
    $x = $x replace '\s',''
    Get-ADUser -Filter {EmailAddress -eq $x}
}

This will pull a Get-ADuser for the entire list by email address. It will also remove white space, which has caused me issues in this situation in the past. Let me know if you have further questions or if you have trouble getting the above commands to work.

Upvotes: 2

rzantarra
rzantarra

Reputation: 89

It doesn't look like you are setting up properties for the search result to return. Ie:

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

Upvotes: 4

Related Questions