I worked with this method several times and it is very useful and efficient when your customer in Office365 and you have AAD Connect in place and they want to change their primary domain with a new one say, the old one is cats.com to dogs.com and you need to deal with a lot of number of mailboxes, the steps are:
- In the Office365 and from the Active Users screen export the users to .csv
- Clean the sheet and leave the UPN and the Primary SMTP Address columns.
- Clean the UPN column by removing everything after the @.
- Add the new email addresses in another column to the sheet.
- Change the heading in the sheet for the UPN to username for old smtp to OldEmail and for the new smtp NewEmail.
- Save the file as users.csv and copy it to the DC.
- Stop the AAD Connect from services till you finish all the process.
- Remove the old primary SMTP email address run the following:
import-csv C:\temp\users.csv | foreach ({Set-ADUser -identity $_.Username -Remove @{proxyAddresses = "SMTP:"+($_.OldEmail)}}) - To add the new primary SMTP
import-csv C:\temp\users.csv | foreach ({Set-ADUser -identity $_.Username -Add @{proxyAddresses = "SMTP:"+($_.NewEmail)}}) - To add the old primary as a secondary SMTP
import-csv C:\temp\usesr.csv | foreach ({Set-ADUser -identity $_.Username -Add @{proxyAddresses = "smtp:"+($_.OldEmail)}}) - To check the changes:
import-csv C:\temp\users.csv | foreach ({Get-ADUser -identity $_.Username -Properties *})| fl DisplayName,proxyaddresses - After everything changed as you wish, start the AAD Connect services and watch the magic!