# Connect to Exchange Online $credential = Get-Credential Connect-ExchangeOnline -Credential $credential # Define the source and target domain $sourceDomain = "olddomain.com" $targetDomain = "newdomain.com" # Prompt for user input to specify a single user $userEmailAddress = Read-Host "Enter the email address of the user to update" # Get the specified user $user = Get-Mailbox -Identity $userEmailAddress # Check if the user exists and if their email address matches the source domain if ($user -ne $null -and $user.PrimarySmtpAddress -like "*@$sourceDomain") { $currentPrimary = $user.PrimarySmtpAddress.ToString() $newPrimary = $currentPrimary.Replace($sourceDomain, $targetDomain) # Confirm the update with the user Write-Host "Updating primary email address for user: $($user.DisplayName)" Write-Host "Current email address: $($user.PrimarySmtpAddress)" Write-Host "New email address: $newPrimary" $confirmation = Read-Host "Do you want to proceed with the update? (Y/N)" if ($confirmation -eq "Y") { # Change the email address for the user Set-Mailbox -Identity $userEmailAddress -PrimarySmtpAddress $newPrimary Write-Host "Primary email address updated successfully." } else { Write-Host "Update canceled." } } else { Write-Host "User not found or their email address doesn't match the source domain." } # Disconnect from Exchange Online Disconnect-ExchangeOnline