For updating AzureAD user's
email address in bulk create a csv "mail.csv" where 1st column
name is UserPrincipalName and the 2nd column name is EmailAddress on
desktop. Filled up the details accordingly.
Open the powershell and install module
Microsoft.Graph by the following command:
Insatall-Module -Name Microsoft.Graph
Connect with module:
Connect-MgGraph -Scopes User.ReadWrite.All, Directory.ReadWrite.All,
Group.ReadWrite.All
Fill up your credential and allow for the permissions.
Now you are connected to MgGraph.
Copy the following script and modify as per your requirements if needed:
# Specify the path to the input CSV file
$csvInputPath = "C:\Users\Administrator\Desktop\mail.csv"
# Specify the path to the output CSV file
$csvOutputPath = "C:\Users\Administrator\Desktop\update_results.csv"
try {
# Import CSV and process each row
$results = Import-Csv $csvInputPath | ForEach-Object {
$UserPrincipalName = $_.UserPrincipalName
$EmailAddress = $_.EmailAddress
$resultObject = [PSCustomObject]@{
UserPrincipalName = $UserPrincipalName
EmailAddress = $EmailAddress
Status = "Not Updated"
}
try {
# Update Microsoft 365 user
Update-MgUser -UserId $UserPrincipalName -Mail $EmailAddress
$resultObject.Status = "Updated"
Write-Host "User '$UserPrincipalName' updated with email '$EmailAddress'
- Done"
} catch {
# Handle errors during user update
$resultObject.Status = "Error: $($_.Exception.Message)"
Write-Host "Error updating user '$UserPrincipalName':
$($_.Exception.Message)"
}
$resultObject
}
# Export results to CSV
$results | Export-Csv -Path $csvOutputPath -NoTypeInformation
Write-Host "Script execution completed - Done"
} catch {
# Handle errors during CSV import
Write-Host "Error importing or exporting CSV file: $_.Exception.Message"
}
Save it on your desktop as mail.ps1.
On powershell, go to the desktop location and run the script by "./mail.ps1".
You will get the result on terminal and a "update_result" named csv will be created on desktop where you can check the per user result.


0 Comments