This script is to remove a smtp domain from a client’s
mailboxes. Email address policy is how these domains get added to the
mailboxes. However email address policies are additive only and cannot be used
to remove the domain that was added using email address policy. They have to
removed manually from each mailbox.
When will we need this?
A good scenario is when a client company ABC has changed
their company smtp domain from abc.com to xyz.com and no longer want to receive
any email on the old smtp abc.com that is they want that if somebody sends an
email to abc.com they should get a bounceback. The old domain abc.com has been removed from the accepted domains and the MX
records for abc.com no longer point to your exchange server. Externally this
will work correctly because obviously the DNS has been modified and abc.com has
been removed from accepted domains in our exchange. But internally on the same
exchange server, users will still be able to send and receive an email to abc.com address. The
email addresses with abc.com either needs to be removed manually from each
users exchange properties or you can use powershell to do it for you.
How will this work?
1 I am using a custom attribute for filtering the
get-mailbox command, but you can use –scope to use OU for filtering or select
all the users. Modify the customattribute1 value to the of the client and
domain name (domain.com) to the target smtp domain in the script and copy the
script
2 Open the exchange management shell and paste the
script in the shell window (press enter once)
All the email address for client with clientcode
‘clientcode’ that contain email addresses with smtp domain ‘domain.com’ will be
removed.
#Script to remove email address for a particular domain as
EAP is additive Only
# BEFORE USING - please change the domain name and custom
attribute as mentioned in comments
# IMPORTANT: DOMAIN IS NOT YOUR AD DOMAIN BUT THE SMTP
DOMAIN YOU WANT TO REMOVE
#Gets the client mailboxes for the users with
customattribute1 set as 'abc'
foreach($Clientmailbox in Get-mailbox -ResultSize Unlimited
| where{$_.CustomAttribute1 -eq 'abc'})
{
#for each mailbox grabs the email addresses and filters the
addresslist
#for the smtp domain the needs to be removed
#and then removes the email address
#CHANGE THE DOMAIN from domain.com to the corresponding
domain
$Clientmailbox.EmailAddresses |
?{$_.AddressString -like '*@domain.com'} | %{
Set-Mailbox
$clientmailbox -EmailAddresses @{remove=$_}
}
}