looking up ad user by email from text file

I have a text file with some email addresses

$text = Get-Content 'C:\Users\tantony\Desktop\email.txt'

foreach ($emails in $text)
{
    $Get = Get-ADUser -Filter {Emailaddress -like "$emails"}    
}

But, when I run this, it’s not displaying anything. The emails I have in the list are valid exchange emails.

Thanks,

The code you posted doesn’t output anything. You are assigning the results to a variable, but you never display that variable.

You should at least have the last user in your variable $Get. :wink:
Try this:

$text = Get-Content ‘C:\Users\tantony\Desktop\email.txt’

foreach ($emails in $text)
{
Get-ADUser -Filter {Emailaddress -like “$emails”} -Properties Emailaddress
}

Thank you,

I tried that, but it still doesn’t display anything. It should be displaying $Get, but it’s not.

$text = Get-Content ‘C:\Users\tantony\Desktop\email.txt’

foreach ($emails in $text)
{
    $Get = Get-ADUser -Filter {Emailaddress -like "$emails"} -Properties Emailaddress     
    $Get  
}

So your text file might be the problem. Can you post a few lines of it here? But you should obfuscate sensitive information.

PS C:\Windows\system32> $text
bgreen@company.com
JMYERS@company.com
smorris@company.com
cramos@company.com
careers@company.com
mmiller@company.com
kadams@company.com
lking@company.com
dsinger@company.com
isandler@company.com

OK, try this:

$text = Get-Content 'C:\Users\tantony\Desktop\email.txt'
foreach ($emails in $text)
{
    $Get = Get-ADUser -Filter {Emailaddress -like $emails} -Properties Emailaddress     
    $Get  
}

same as before, doesn’t display $Get

hmmm … I tried it and it’s been working for me.

found the problem, I had some “white spaces”

Don’t need to do this
$Get = Get-ADUser -Filter {Emailaddress -like $emails} -Properties Emailaddress
$Get

as
Get-ADUser -Filter {Emailaddress -like $emails} -Properties Emailaddress

will display the result