I'm struggling with getting the SamAccountName from AD by searching with the email address. I have a CSV that contains email addresses but need to get the SamAccountName. "write-host $user.EmailAddress" prints the email address correctly, Get-ADuser is not giving any errors but "write-host $CurrentUser" prints an empty line
foreach($user in $users)
{
write-host $user.EmailAddress
#$CurrentUser = Get-ADUser -filter "EmailAddress -eq '$($user)'"
#$CurrentUser = Get-aduser -Filter { "mail -eq '$($user.EmailAddress)'"} -Properties mail
$CurrentUser = Get-ADUser -Filter { mail -eq "$($user.EmailAddress)"} -properties SamAccountName
write-host $CurrentUser
I've tried a few different snippets I found online but just can't get it to work
Powershell version: 5
Get-ADUser
with just one entry from the$Users
list, does it work? – Lee_Dailey-Filter "property -operator 'value'"
always works – AdminOfThings