Get-Mailbox -Identity $userprincipalname | # Loop through all the emailaddresses foreach { $a = $_.emailaddresses $b = $_.emailaddresses # Remove all but $keepmail foreach($e in $a) { if ($e.tostring() -notmatch $keepmail ) { $b -= $e; } $_ | Set-mailbox -EmailAddressPolicyEnabled $false -emailaddresses $b -alias $userprincipalname } } # We had to remove the emailaddresspolicy to make changes. Let's reactivate it Set-mailbox -Identity $userprincipalname -EmailAddressPolicyEnabled $true }
Delete all email addresses but one on a mailbox
Leave a reply