Exchange user and distribution group SMTP cleanup with Powershell

Are there any out-of-date user mailbox or distribution group SMTP entries in your Exchange e-mail environment? Have you removed an existing Exchange ‘e-mail address policy’ but the old SMTP entry remains across the organisation?

Perhaps you are about to migrate identities into the MS Azure cloud using MS AADConnect, and you are cleaning up local AD objects. The Microsoft idFix tool can check your local AD user and group OUs to determine any issues with duplicate entries, invalid characters and old SMTP entries etc. If there are any SMTP issues reported, you may need to programatically remove invalid/old/disused SMTP entries from your Exchange e-mail environment. There is no better way than to use Powershell.

The example scripts assume that the following undesirable SMTP entries need to be removed:

– all or most AD users have a name.lastname@legacy.migration SMTP entry
– all of most distribution groups have a distname@internal.local SMTP entry

The objective of the Powershell scripts is to only target and remove the undesirable SMTP entries, and to log the deleted entry to a text file.

Here is what the first ‘user mailbox’ script looks like (normally saved as script.ps1 and run in a Powershell window that has been launched to ‘run as administrator’):

[ps]
$mailboxes=get-mailbox -resultsize unlimited

foreach($mailbox in $mailboxes) {
for($i=($mailbox.EmailAddresses.count)-1; $i -ge 0; $i–) {
$address=$mailbox.EmailAddresses[$i]
$addressString=$address.addressString

if($addressString -like "*@legacy.migration") {
$addressString | Out-File "c:\temp\smtpcleanup.txt" -Append
Write-host("Removed smtp address: " + $mailbox )
$mailbox.EmailAddresses.removeat($i)
}
}
$mailbox|set-mailbox -EmailAddresses $mailbox.EmailAddresses
}
[/ps]

The ‘distribution groups’ can be targeted and cleaned up using the following PS script:

[ps]
$mailboxes=get-distributiongroup -resultsize unlimited

foreach($mailbox in $mailboxes) {
for($i=($mailbox.EmailAddresses.count)-1; $i -ge 0; $i–) {
$address=$mailbox.EmailAddresses[$i]
$addressString=$address.addressString

if($addressString -like "*@internal.local") {
$addressString | Out-File "c:\temp\distgroupsmtpcleanup.txt" -Append
Write-host("Removed smtp address: " + $mailbox )
$mailbox.EmailAddresses.removeat($i)
}
}
$mailbox|set-distributiongroup -EmailAddresses $mailbox.EmailAddresses
}
[/ps]

Both scripts work almost identically, and are differentiated only by the get-mailbox command for regular mailbox targeting, and the get-distributiongroup command for dist groups.

– both scripts iterate through all available mailboxes and dist groups
– SMTP addresses are compared using the -like command to the undesirable SMTP suffix wildcard
– deleted SMTP entries are logged to a text file using the Out-File command

Happy and safe travels with your SMTP cleanups!

Have you achieved this in a better/cleaner way? Let me know.

Leave a Reply

Your email address will not be published. Required fields are marked *