Exchange Powershell: Remove e-mail address from address list

This powershell script is written for Microsoft Exchange 2010.

Problem
I recently came across a problem where all exchange mailboxes contained a number of address aliases that should not be there. The additional SMTP addresses were not created as part of an address policy so there was no simple way to remove them without manually removing them from each individual mailbox (None that I knew of anyway)

Solution
I created a simple powershell script that will loop through all mailboxes in an Exchange setup checking their list of email addresses. If the email address ends with the domain name that is requested when you run the script, it will remove the entire address from the mailbox.
Put simply, this script will remove any email address that contains a given domain name.
So please be careful when using.

#requires -version 2 

#Custom variables 
$mailboxes = Get-Mailbox
$domain = read-host "Domain Name to remove @"
$domain = $domain.ToLower()
 
#Loop through all mailboxes 
foreach ($mailbox in $mailboxes) { 
  #Loop through email addresses
  foreach ($address in $mailbox.EmailAddresses){
	$addr=$address.SmtpAddress
	if ($addr){
	  $addr=$addr.ToLower()
	  if ($addr.EndsWith($domain)){
		Set-Mailbox $mailbox -EmailAddresses @{Remove=$addr}
	  }
	}
  }
}

7 thoughts on “Exchange Powershell: Remove e-mail address from address list”

  1. I run the script however the domain is not removed. I was looking at the variables and they don’t seem to be correct. Shouldn’t $adress really be $_.address and so fourth? Also, some parameters seem to be missing.

  2. Hi,

    It’s not working for me, I have got this issue :

    Set-Mailbox : Cannot bind parameter ‘EmailAddresses’. Cannot convert the “System.Collections.Hashtable” value of type “System.Collections.Hashtable”
    to type “Microsoft.Exchange.Data.ProxyAddress”.
    At line:10 char:61
    + Set-Mailbox $mailbox -EmailAddresses <<<< @{Remove=$addr} -whatif
    + CategoryInfo : InvalidArgument: (:) [Set-Mailbox], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Exchange.Management.RecipientTasks.SetMailbox

Comments are closed.