Office 365 remove SMTP address off all accounts in bulk (800 accounts)

We have an Office 365 tenant which has multiple verified domains and those domains are assigned to the mailboxes.  We need to remove one of the domains from the portal.  The accounts which are assigned the domain that we want to remove only have this domain assigned to their mailbox and the .onmicrosoft.com domain as well.  We want all the accounts to be left with only the .onmicrosoft.com domain as the sign in ID and the primary SMTP address.

We will need to:

1- Change the user's sign in ID to .onmicrosoft.com.
2- Change the user's primary SMTP address to .onmicrosoft.com.
3- Remove the verified domain from the user's mailboxes without affecting the other accounts and their sign-in IDs and other validated SMTP addresses.

I have #1 down:  Get-Msoluser -DomainName "MyValidatedDomain.com" |%{Set-MsolUserPrincipalName -UserPrincipalName $_.UserPrincipalName -NewUserPrincipalName ($_.userprincipalname.split("@")[0]+"@MyMSDomain.onmicrosoft.com");Write-Host "UPN Has been updated for" $_.DisplayName}

Could someone help with #2 and #3?  Remember, we can't affect the other accounts with other verified SMTP domains, just the ones with the SMTP domain that we want to remove.

Thanks,

Julian
lighthousekeeperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vasil Michev (MVP)Commented:
There's a script on TechNet Gallery which you can use for that: https://gallery.technet.microsoft.com:443/How-to-remove-a-custom-6fe91693

If I remember correctly, the script does not change the primary SMTP, so you can add this part:

Set-Mailbox -Identity user@domain.com -WindowsEmailAddress user@domain.onmicrosoft.com

Open in new window


The cmdlet will make user@domain.onmicrosoft.com the primary SMTP address and copy the old primary one as secondary. To make this work in bulk, you can just use something like: $_.alias + "@domain.onmicrosoft.com"

After you do this, you can simply run the script and it will strip all aliases associated with @domain.com

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Office 365

From novice to tech pro — start learning today.