Get Samaccountname from email

Alex
Alex used Ask the Experts™
on
Afternoon

My brain is fried, I'm feeling rough and sweating loads.

$users = Import-csv C:\temp\Manager.csv
foreach ($User in $Users){

 Get-aduser -Properties * -Filter {(mail -like $_.'Line Manager Email')}}

Open in new window



Why isn't this working?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Most likely reason: you're using the ForEach statement with a named loop variable, not the ForEach-Object cmdlet, but are using "$_" as loop variable in the filter. Change either the loop type or the variable and try again.
AlexSenior Infrastructure Analyst

Author

Commented:
So something like this?

Import-csv C:\temp\Manager.csv | foreach-object | Get-aduser -Properties * -Filter {(mail -like $_.'Line Manager Email')}

I tried that and it still didn't like it :(
AlexSenior Infrastructure Analyst

Author

Commented:
I also tried

$users = Import-csv C:\temp\Manager.csv
foreach ($User in $Users){

 Get-aduser -Properties * -Filter {(mail -like $user.'Line Manager Email')}}

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
This works for me:
$users = Import-Csv C:\temp\Manager.csv
$Users | ForEach-Object {Get-ADUser -Properties * -Filter "mail -like '$($_.'Line Manager Email')'"}

Open in new window

AlexSenior Infrastructure Analyst

Author

Commented:
Urgh, powershell is silly.... :(

Thank you, at least it wasn't entirely me being stupid.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial