Powershell noob here trying to get a list of UPN's from AAD using only a csv with email address and failing any help pointing me in the correct direction;
I can obtain the UPN from a single email address using this code;
$User = "email@example.com"
Get-AzureADUser -Filter "startswith(Mail,'$User')" | Select-Object DisplayName,UserPrincipalName,Mail,ObjectId | Export-csv -append -notypeinformation “C:\Temp\output.csv”
My attempt to do this for multiple email addresses in a csv fails here is what i have so far;
$Result = @()
$AllUsers = Import-Csv "C:\Temp\Users.csv"
ForEach ($User in $AllUsers)
{
Get-AzureADUser -Filter "startswith(Mail,'$Users')" | Select-Object DisplayName,UserPrincipalName,Mail,ObjectId
$Result += New-Object PSObject -property $([ordered]@{
UserName = $User.DisplayName
UserPrincipalName = $User.UserPrincipalName
PrimarySmtpAddress = $User.Mail
AliasSmtpAddresses = ($User.ProxyAddresses | Where-Object {$_ -clike 'smtp:*'} | ForEach-Object {$_ -replace 'smtp:',''}) -join ','
UserId= $User.ObjectId
})
}
$Result | Export-CSV "C:\Temp\output.csv" -NoTypeInformation -Encoding UTF8
any suggestions where i am failing in the above or recommendations on how to do this better?