8

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

Get-ADUser -Filter {EmailAddress -eq 'jperez@dominio.com'}

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

mklement0
  • 382,024
  • 64
  • 607
  • 775
Hans Torres
  • 81
  • 1
  • 1
  • 2
  • Welcome to Stack Overflow! Unfortunately your question is very unclear. Include suitable tags to show us which language/technology you are using. Also refer to https://stackoverflow.com/help/how-to-ask to learn how to ask a question of high quality. Also correctly format your code using the markdown syntax (help can be found in the editor) – akraf Dec 19 '17 at 22:19
  • Does this answer your question? [Why doesn't $PSItem behave as expected when using a bracket-based -Filter argument?](https://stackoverflow.com/questions/51137464/why-doesnt-psitem-behave-as-expected-when-using-a-bracket-based-filter-argume) – mklement0 Mar 18 '20 at 18:47
  • 1
    While seductively convenient, it's best to avoid the use of script blocks (`{ ... }`) as `-Filter` arguments - and their use is precisely the problem here: you cannot use a _property reference_ such as `$_.email` in the filter this way. - see [this answer](https://stackoverflow.com/a/44184818/45375) to a question that your question is a duplicate of. – mklement0 Mar 18 '20 at 18:50

4 Answers4

2

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 
}
rzantarra
  • 69
  • 1
  • 8
2

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.

0

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 :)

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
0

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 $_}} 
Andrew Maiman
  • 111
  • 1
  • 5