Add Proxy Addresses for Recipients by Using a CSV file in Microsoft Exchange 2010

Introduction

This script could be used to add proxy addresses for recipients by using a CSV file in Microsoft Exchange 2010. You can export the recipients which you want to add new proxy address by using this script also.

Scenarios

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

Script

This script contains two advanced functions, Out-OSCEXProxyAddressFile and Add-OSCEXProxyAddress. 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 Add-OSCEXProxyAddress 
{ 
    [CmdletBinding(SupportsShouldProcess=$true)] 
    Param 
    ( 
        #Define parameters 
        [Parameter(Mandatory=$true,Position=1)] 
        [string]$CsvPath     
    ) 
    Process 
    { 
        if (Test-Path -Path $CsvPath -PathType Leaf -Filter "*.csv") { 
            $recipients = Import-Csv $CsvPath 
            Try 
            { 
                $isValidInputFile = ($recipients | Get-Member -MemberType NoteProperty -Name Alias -ErrorAction Stop) -ne $null 
                $isValidInputFile = $isValidInputFile -and (($recipients | Get-Member -MemberType NoteProperty -Name NewProxyAddress -ErrorAction Stop) -ne $null) 
            } 
            Catch 
            { 
                #Supress error: No object has been specified to the get-member cmdlet. 
                #If the .CSV file only contains column headers, this error will occur. 
            } 
            if (-not $isValidInputFile) { 
                $errorMsg = $Messages.SpecifyQualifiedInputFile 
                $customError = New-OSCPSCustomErrorRecord ` 
                -ExceptionString $errorMsg ` 
                -ErrorCategory NotSpecified -ErrorID 1 -TargetObject $pscmdlet 
                $pscmdlet.WriteError($customError) 
                return $null 
            } 
        } else { 
            $errorMsg = $Messages.SpecifyQualifiedInputFile 
            $customError = New-OSCPSCustomErrorRecord ` 
            -ExceptionString $errorMsg ` 
            -ErrorCategory NotSpecified -ErrorID 1 -TargetObject $pscmdlet 
            $pscmdlet.WriteError($customError) 
            return $null             
        } 
        #Get recipient 
        foreach ($identity in $recipients) { 
            $recipient = Get-Recipient -Identity $identity.Alias -Verbose:$false 
            if ($recipient -ne $null) { 
                $recipientAlias = $recipient.Alias 
                #Check recipient type, then use different cmdltes for adding proxy address 
                Switch -regex ($recipient.RecipientType) 
                { 
                    "UserMailbox" { 
                        $mailbox = Get-Mailbox -Identity $recipientAlias -Verbose:$false 
                        #Process multiple proxy addresses 
                        if ($identity.NewProxyAddress.Contains(",")) { 
                            $nAddresses = $identity.NewProxyAddress.Split(",") 
                        } else { 
                            $nAddresses = $identity.NewProxyAddress 
                        } 
                        #Add proxy address for Mailbox 
                        foreach ($nAddress in $nAddresses) { 
                            if ($mailbox.EmailAddresses -notcontains $nAddress) { 
                                #Use ShouldProcess method for supporting -Whatif parameter 
                                if ($pscmdlet.ShouldProcess($recipientAlias)) { 
                                    $verboseMsg = $Messages.AddingProxyAddress 
                                    $verboseMsg = $verboseMsg -replace "Placeholder01",$nAddress 
                                    $verboseMsg = $verboseMsg -replace "Placeholder02",$recipientAlias 
                                    $pscmdlet.WriteVerbose($verboseMsg) 
                                    $newAddress = $mailbox.EmailAddresses.Add($nAddress) 
                                    Set-Mailbox -Identity $recipientAlias -EmailAddresses $mailbox.EmailAddresses -Verbose:$false 
                                } 
                            } else { 
                                $warningMsg = $Messages.NewProxyAddressExists 
                                $warningMsg = $warningMsg -replace "Placeholder01",$nAddress 
                                $warningMsg = $warningMsg -replace "Placeholder02",$recipientAlias 
                                $pscmdlet.WriteWarning($warningMsg)                         
                            } 
                        } 
                    } 
                    "MailUser" { 
                        $mailUser = Get-MailUser -Filter "Alias -eq `"$recipientAlias`"" -Verbose:$false 
                        #Process multiple proxy addresses 
                        if ($identity.NewProxyAddress.Contains(",")) { 
                            $nAddresses = $identity.NewProxyAddress.Split(",") 
                        } else { 
                            $nAddresses = $identity.NewProxyAddress 
                        } 
                        #Add proxy address for MailUser 
                        foreach ($nAddress in $nAddresses) {                     
                            if ($mailUser.EmailAddresses -notcontains $nAddress) { 
                                #Use ShouldProcess method for supporting -Whatif parameter 
                                if ($pscmdlet.ShouldProcess($recipientAlias)) { 
                                    $verboseMsg = $Messages.AddingProxyAddress 
                                    $verboseMsg = $verboseMsg -replace "Placeholder01",$nAddress 
                                    $verboseMsg = $verboseMsg -replace "Placeholder02",$recipientAlias 
                                    $pscmdlet.WriteVerbose($verboseMsg) 
                                    $newAddress = $mailUser.EmailAddresses.Add($nAddress) 
                                    Set-MailUser -Identity $recipientAlias -EmailAddresses $mailUser.EmailAddresses -Verbose:$false 
                                } 
                            } else { 
                                $warningMsg = $Messages.NewProxyAddressExists 
                                $warningMsg = $warningMsg -replace "Placeholder01",$nAddress 
                                $warningMsg = $warningMsg -replace "Placeholder02",$recipientAlias 
                                $pscmdlet.WriteWarning($warningMsg)                         
                            } 
                        } 
                    } 
                    "MailContact" { 
                        $mailContact = Get-MailContact -Filter "Alias -eq `"$recipientAlias`"" -Verbose:$false 
                        #Process multiple proxy addresses 
                        if ($identity.NewProxyAddress.Contains(",")) { 
                            $nAddresses = $identity.NewProxyAddress.Split(",") 
                        } else { 
                            $nAddresses = $identity.NewProxyAddress 
                        } 
                        #Add proxy address for Contact 
                        foreach ($nAddress in $nAddresses) {                     
                            if ($mailContact.EmailAddresses -notcontains $nAddress) { 
                                #Use ShouldProcess method for supporting -Whatif parameter 
                                if ($pscmdlet.ShouldProcess($recipientAlias)) { 
                                    $verboseMsg = $Messages.AddingProxyAddress 
                                    $verboseMsg = $verboseMsg -replace "Placeholder01",$nAddress 
                                    $verboseMsg = $verboseMsg -replace "Placeholder02",$recipientAlias 
                                    $pscmdlet.WriteVerbose($verboseMsg) 
                                    $newAddress = $mailContact.EmailAddresses.Add($nAddress) 
                                    Set-MailContact -Identity $recipientAlias -EmailAddresses $mailContact.EmailAddresses -Verbose:$false 
                                } 
                            } else { 
                                $warningMsg = $Messages.NewProxyAddressExists 
                                $warningMsg = $warningMsg -replace "Placeholder01",$nAddress 
                                $warningMsg = $warningMsg -replace "Placeholder02",$recipientAlias 
                                $pscmdlet.WriteWarning($warningMsg)                         
                            } 
                        } 
                    } 
                    "DistributionGroup" { 
                        #Process multiple proxy addresses 
                        if ($identity.NewProxyAddress.Contains(",")) { 
                            $nAddresses = $identity.NewProxyAddress.Split(",") 
                        } else { 
                            $nAddresses = $identity.NewProxyAddress 
                        } 
                        #Add proxy address for distribution group 
                        foreach ($nAddress in $nAddresses) {                 
                            if ($recipient.RecipientType -like "Mail*" ) { 
                                $mailDG = Get-DistributionGroup -Identity $recipientAlias -Verbose:$false 
                            } else { 
                                $mailDG = Get-DynamicDistributionGroup -Identity $recipientAlias -Verbose:$false 
                            }     
                            if ($mailDG.EmailAddresses -notcontains $nAddress) { 
                                #Use ShouldProcess method for supporting -Whatif parameter 
                                if ($pscmdlet.ShouldProcess($recipientAlias)) { 
                                    $verboseMsg = $Messages.AddingProxyAddress 
                                    $verboseMsg = $verboseMsg -replace "Placeholder01",$nAddress 
                                    $verboseMsg = $verboseMsg -replace "Placeholder02",$recipientAlias 
                                    $pscmdlet.WriteVerbose($verboseMsg) 
                                    $newAddress = $mailDG.EmailAddresses.Add($nAddress) 
                                    if ($recipient.RecipientType -like "Mail*" ) { 
                                        Set-DistributionGroup -Identity $recipientAlias -EmailAddresses $mailDG.EmailAddresses -Verbose:$false 
                                    } else { 
                                        Set-DynamicDistributionGroup -Identity $recipientAlias -EmailAddresses $mailDG.EmailAddresses -Verbose:$false 
                                    } 
                                } 
                            } else { 
                                $warningMsg = $Messages.NewProxyAddressExists 
                                $warningMsg = $warningMsg -replace "Placeholder01",$nAddress 
                                $warningMsg = $warningMsg -replace "Placeholder02",$recipientAlias 
                                $pscmdlet.WriteWarning($warningMsg)                         
                            } 
                        } 
                    } 
                } 
            } else { 
                $errorMsg = $Messages.CannotFindRecipient 
                $errorMsg = $errorMsg -replace "Placeholder01",$Identity 
                $customError = New-OSCPSCustomErrorRecord ` 
                -ExceptionString $errorMsg ` 
                -ErrorCategory NotSpecified -ErrorID 1 -TargetObject $pscmdlet 
                $pscmdlet.WriteError($customError) 
                return $null         
            } 
        } 
    } 
} 
 
 

 

Examples

Example 01: Displays help about Add-OSCEXProxyAddress
Command: Get-Help Add-OSCEXProxyAddress -Full
Screenshot:

 

Example 02: Prepare input file by using Out-OSCEXProxyAddressFile, the following command will export the recipients which alias starts with "New". Example01.com and example02.com are the new proxy address domains which will be added, it will use existing alias of the recipient.
Command:
 Out-OSCEXProxyAddressFile -Path "c:\Scripts\030\userinput.csv" -DomainName "example01.com","example02.com" -Filter 'Alias -like "New*"'
Screenshot:

 

Example 03: Prepare input file by using Out-OSCEXProxyAddressFile, the following command will export the recipients which alias starts with "New" and recipient type is MailUniversalDistributionGroup. Example01.com and example02.com are the new proxy address domains which will be added, it will use existing alias of the recipient.
Command:
 Out-OSCEXProxyAddressFile -Path "c:\Scripts\030\userinput.csv" -DomainName "example01.com","example02.com" -Filter '(Alias -like "New*") -and (RecipientType -eq "MailUniversalDistributionGroup")'
Screenshot:

 

Example 04: Add new proxy address base on the input file.
Command:
 Add-OSCEXProxyAddress -CsvPath c:\Scripts\030\userinput.csv -Verbose
Screenshot:

 

 

Additional Resources

Technical Resource:

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

Get-Mailbox
http://technet.microsoft.com/en-us/library/bb123685.aspx

Get-Contact
http://technet.microsoft.com/en-us/library/aa998258.aspx

Get-DistirbutionGroup
http://technet.microsoft.com/en-us/library/bb124755.aspx

Get-Recipient
http://technet.microsoft.com/en-us/library/aa996921.aspx

Filterable Properties for the -Filter Parameter
http://technet.microsoft.com/en-us/library/bb738155(v=exchg.90).aspx

Forum Threads:

Add proxyAddresses for 1000+ users?
http://social.technet.microsoft.com/Forums/ar/exchangesvradmin/thread/9fdf3f7c-cffb-43cd-a8a6-dbbde850c29c

Need to add alias email addresses to 1200 accounts
http://social.technet.microsoft.com/Forums/en/exchangesvradmin/thread/99a696ad-59cc-47ec-a9b0-9790ab2908e7

Adding proxyAddresses with Powershell
http://social.technet.microsoft.com/Forums/en/exchangesvradmin/thread/10822d3d-fe16-4bcd-91f2-c757a800a6fc