Connect-AzureAD
# Define old and new domains
$oldDomain = "domain.com.au"
$newDomain = "cfch2020.onmicrosoft.com"
# Get all users with the old domain in their UPN
$users = Get-AzureADUser -All $true | Where-Object { $_.UserPrincipalName -like "*@$oldDomain" }
foreach ($user in $users) {
# Construct the new UPN
$newUPN = $user.UserPrincipalName -replace "@$oldDomain", "@$newDomain"
# Update the user's UPN
Write-Host "Updating UPN for $($user.UserPrincipalName) to $newUPN"
Set-AzureADUser -ObjectId $user.ObjectId -UserPrincipalName $newUPN
}
Write-Host "UPN update completed."