PS - Remove Legacy Email Addresses


Recently during an Exchange Online migration a project, I found an internal domain name in the default email address policy, this meant errors were being thrown during the migration to Exchange Online. Although the internal domain name was removed from the email address policy, this does not delete any existing email address that have already been applied. 

I needed a script to remove all email addresses relating to an legacy domain name. 

In the example the domain is DOMAIN.LOCAL 



BACKUP


first of All I backed up all the email address configured to a CSV file


Get-Mailbox -ResultSize Unlimited | select -expand emailaddresses alias | EXPORT-CSV d:\exchangemigration\emailaddressesbackup.csv 


Get-Mailbox -ResultSize Unlimited | select -expand emailaddresses alias | EXPORT-CSV emailaddressesbackup.csv 

Download RemoveEmailAddress.zip from - https://gallery.technet.microsoft.com/office/Remove-E-Mail-address-with-d716e055  andeExtract the content to \\Server\d$\ExchangeMigraiton


TEST


Now I tested the script for a few test accounts I had created:

Open the script file with Notepad. Scroll down to the end of the script file, and then add the command:

Remove-OSCEXEmailAddress -Filter '(Alias -like "Neil PetersenTest*") -and (RecipientType -eq "UserMailbox")' -DomainName "DOMAIN.LOCAL" –Verbose -Whatif

Save the file then run the script in Exchange Management Shell.

Ensure that the -whatif output shows the email address being removed from the accounts as expected.

Open the script file with Notepad. Scroll down to the end of the script file, and then add the command:

Remove-OSCEXEmailAddress -Filter '(Alias -like "Neil PetersenTest*") -and (RecipientType -eq "UserMailbox")' -DomainName "DOMAIN.LOCAL" –Verbose 

Save the file then run the script in Exchange Management Shell.

Ensure that the output shows the email address being removed from the accounts as expected.




DEPLOY


Open the script file with Notepad. Scroll down to the end of the script file, and then add the command:

Remove-OSCEXEmailAddress -Filter '(Alias -like "*") -and (RecipientType -eq "UserMailbox")' -DomainName "DOMAIN.LOCAL" –Verbose -Whatif

Save the file then run the script in Exchange Management Shell.

Ensure that the –whatif output shows the email address being removed from the accounts as expected.

Open the script file with Notepad. Scroll down to the end of the script file, and then add the command:

Remove-OSCEXEmailAddress -Filter '(Alias -like "*") -and (RecipientType -eq "UserMailbox")' -DomainName "DOMAIN.LOCAL" –Verbose

Save the file then run the script in Exchange Management Shell.

Ensure that the output shows the email address being removed from the accounts as expected.

Comments