Remove E-Mail address with Specific Domain Name in Microsoft Exchange 2010

Introduction

This script could be used to remove E-Mail address with specific domain Name in Microsoft Exchange 2010. This script can remove E-Mail address from mailbox, distribution group and dynamic distribution group.

Scenarios

In a real world, IT Administrators may want to remove new e-mail addresses due to variety of reasons. If an organization has thousands of recipients, it’s impossible to remove e-mail addresses for these recipients one by one. IT administrators do need a script to complete this task.

Script

This script contains one advanced function, Remove-OSCEXEmailAddress. You can use this script in following ways:

Method 1:

  1. Download the script and copy it to a Microsoft Exchange 2010 Server.
  2. Open the script file with Notepad or any other script editors.
  3. Scroll down to the end of the script file, and then add the example command which you want to run.
  4. Save the file then run the script in Exchange Management Shell.

Method 2:

  1. Rename scriptname.ps1 to scriptname.psm1 (PowerShell Module file)
  2. Run Import-Module cmdlet to import this module file in Exchange Management Shell.
     Import-Module filepath\scriptname.psm1

 

PowerShell
Edit|Remove
Function Remove-OSCEXEmailAddress 
{ 
    [CmdletBinding(SupportsShouldProcess=$true)] 
    Param 
    ( 
        #Define parameters 
        [Parameter(Mandatory=$true,Position=1)] 
        [string]$Filter, 
        [Parameter(Mandatory=$true,Position=2)] 
        [string[]]$DomainName 
    ) 
    Process 
    { 
        #Get mailboxes or distribution groups 
        $recipients = Get-Recipient -Filter $Filter -ResultSize unlimited -Verbose:$false 
        if ($recipients -ne $null) { 
            foreach ($recipient in $recipients) {         
                if ($recipient.RecipientType -notmatch "UserMailbox|DistributionGroup") { 
                    $warningMsg = $Messages.RecipientTypeIsNotSupported 
                    $warningMsg = $warningMsg -replace "Placeholder01",$($recipient.RecipientType) 
                    $pscmdlet.WriteWarning($warningMsg)                 
                } else { 
                    for ($i=0;$i -lt $recipient.EmailAddresses.Count;$i++) { 
                        #Begin to remove invalid email address one by one. 
                        $addressDomain = $recipient.EmailAddresses[$i].AddressString.Split("@")[1] 
                        if ($DomainName -contains $addressDomain) { 
                            $primarySmtpAddress = $recipient.PrimarySmtpAddress.ToString() 
                            $invalidEmailAddress = $($recipient.EmailAddresses[$i]) 
                            #Use ShouldProcess method for supporting -Whatif parameter 
                            if ($pscmdlet.ShouldProcess($($recipient.Alias))) { 
                                if ($invalidEmailAddress -ne $primarySmtpAddress) { 
                                    $verboseMsg = $Messages.RemoveAddress 
                                    $verboseMsg = $verboseMsg -replace "Placeholder01",$($recipient.EmailAddresses[$i]) 
                                    $pscmdlet.WriteVerbose($verboseMsg) 
                                    Switch ($recipient.RecipientType) { 
                                        "UserMailbox" { 
                                            $mailbox = Get-Mailbox -Identity $($recipient.Alias) -Verbose:$false 
                                            $returnValue = $mailbox.EmailAddresses.Remove($invalidEmailAddress) 
                                            if ($returnValue) { 
                                                Set-Mailbox -Identity $mailbox -EmailAddresses $mailbox.EmailAddresses -Verbose:$false 
                                            } 
                                        } 
                                        "MailUniversalDistributionGroup" { 
                                            $distributionGroup = Get-DistributionGroup -Identity $($recipient.Alias) -Verbose:$false 
                                            $returnValue = $distributionGroup.EmailAddresses.Remove($invalidEmailAddress) 
                                            if ($returnValue) {  
                                                Set-DistributionGroup -Identity $distributionGroup -EmailAddresses $distributionGroup.EmailAddresses -Verbose:$false                                     
                                            } 
                                        } 
                                        "DynamicDistributionGroup" { 
                                            $dynamicDistributionGroup = Get-DynamicDistributionGroup -Identity $($recipient.Alias) -Verbose:$false 
                                            $returnValue = $dynamicDistributionGroup.EmailAddresses.Remove($invalidEmailAddress) 
                                            if ($returnValue) { 
                                                Set-DynamicDistributionGroup -Identity $dynamicDistributionGroup -EmailAddresses $dynamicDistributionGroup.EmailAddresses -Verbose:$false                                     
                                            } 
                                        } 
                                    } 
                                } else { 
                                    #Remove primary SMTP address is not supported by this function. 
                                } 
                            } 
                        } 
                    } 
                } 
            } 
        } else { 
            $errorMsg = $Messages.CannotFindRecipient 
            $customError = New-OSCPSCustomErrorRecord ` 
            -ExceptionString $errorMsg ` 
            -ErrorCategory NotSpecified -ErrorID 1 -TargetObject $pscmdlet 
            $pscmdlet.WriteError($customError) 
            return $null 
        } 
    } 
} 
 

 

Examples

Example 01: Displays help about Remove-OSCEXEmailAddress
Command: Get-Help Remove-OSCEXEmailAddress -Full
Screenshot:

 

Example 02: Confirm the operations which will be executed by using -Whatif parameter.
Command:
 Remove-OSCEXEmailAddress -Filter 'Alias -like "New*"' -DomainName "example01.com" -Whatif
Screenshot:

 

Example 03: Remove email address which contains specific domain name from mailbox.
Command:
 Remove-OSCEXEmailAddress -Filter '(Alias -like "New*") -and (RecipientType -eq "UserMailbox")' -DomainName "example02.com" -Verbose
Screenshot:

 

Example 04: Remove email address which contains specific domain name from distribution group.
Command:
 Remove-OSCEXEmailAddress -Filter '(Alias -like "New*") -and (RecipientType -eq "MailUniversalDistributionGroup")' -DomainName "example02.com" -Verbose
Screenshot:

 

Example 05: Remove email address which contains specific domain name from dynamic distribution group.
Command:
 Remove-OSCEXEmailAddress -Filter '(Alias -like "New*") -and (RecipientType -eq "DynamicDistributionGroup")' -DomainName "example02.com" -Verbose
Screenshot:

 

 

Additional Resources

Technical Resources:

Windows PowerShell Advanced Function
http://technet.microsoft.com/en-us/library/dd315326.aspx

Filterable Properties for the -Filter Parameter
http://technet.microsoft.com/en-us/library/bb738155.aspx

Forum Threads:

Remove email addresses with a particular domain in users’ profile
http://social.technet.microsoft.com/Forums/en-US/exchangesvradmin/thread/34f989aa-cf37-41bf-934c-015b97fded76/

Is there a way to delete them automatically?
http://www.networksteve.com/exchange/topic.php/Is_there_a_way_to_delete_them_automatically/?TopicId=4033&Posts=4

Remove secondary email address of a lists of users with powershell
http://social.technet.microsoft.com/Forums/en-US/exchangesvradmin/thread/4ca1f6dd-07a9-44ac-a32f-f63d42cfc7d1

Delete secondary SMTP address input from a text file using powershell
http://social.technet.microsoft.com/Forums/en-US/exchangesvrtransport/thread/41b041b5-ebdb-459f-b78d-eae4665f9998