I can pull a single username from AD by searching for the email address, but I would like to pull multiple email addresses from the CSV and log them in an output file. I know I need to broaden the search here: mail -eq '[email protected]' with something such as -like or a variable but I'm having difficulty finding a solution.
Code:
$InputFile = 'C:\emailaddresses.csv'
$Outputfile = 'C:\usernames.csv'
Import-CSV -Path $InputFile
| ForEach-Object { Get-ADUser -Filter { mail -eq '[email protected]' } | select SamAccountName }
| Export-Csv $OutputFile -NoTypeInformation
the answer is probably spacing and AD calls are a pain note the
$EmailI assume the header is email in the first csv and I am not sure if you want
-eqor-likebut my answer here will get you there.I tested this on my machine and it works.