≡ Menu

How to Add Exchange Proxy Addresses from a CSV File with PowerShell

I recently had the opportunity to give a presentation on the Idera PowerShell Power Hour Webcast. The topic was Exchange Enterprise Management with PowerShell, and I was excited to be able to answer so many great questions during the Q and A portion of the webcast. One request for help required a more lengthy explanation that I couldn’t provide via web chat, and that’s the reason for this post. If you’ve ever wanted to add one or more proxy address to a list of mailboxes from a CSV file, the solution provided here is for you.

First let me say that adding proxy addresses to multiple recipients can be done even easier with E-Mail Address Policies. You can read all about them here. However, if you are determined to get this done using PowerShell, it’s actually pretty simple to do. I am going to assume you are at least running Exchange 2010 SP1, and that will allow us to utilize this cool hash table trick that can be used to add and remove values from multivalued properties.

Let’s say that you have a CSV file setup like the following:

The Name column shown above contains aliases for existing mailbox-enabled users. The ProxyAddresses column contains a semi-colon separated list of addresses that need to be added to each associated mailbox. The key to making this work is converting the ProxyAddresses value into a collection of addresses. We can use the Import-CSV cmdlet to read our source file, and for each record, we’ll process the list of ProxyAddresses by adding them to each corresponding mailbox. Let’s start first with a simple example:

PS C:\> Import-Csv C:\AddressList.csv | ForEach-Object{$_.ProxyAddresses -split ';'}
ds@domain1.com
dave@domain2.com
sd@domain1.com
steve@domain2.com

Notice that as we loop through each record, we take the value assigned to the ProxyAddresses column and split it into an array using the PowerShell v2 split operator. When we split this value, we can simply iterate over each item returned and the add proxy addresses to the mailbox. Here’s our final code sample that will get this job done:

Import-Csv C:\AddressList.csv | ForEach-Object{
  $name = $_.Name
  $proxy = $_.ProxyAddresses -split ';'
  Set-Mailbox -Identity $name -EmailAddresses @{add= $proxy}
}

The -EmailAddresses parameter is available for cmdlets that modify other recipient types. If needed, replace the call to Set-Mailbox with Set-MailUser, Set-MailContact, etc. and use the same kind of syntax to add proxy addresses.

19 comments… add one

  • Charles Derber December 4, 2011, 12:49 am

    Checked your archived videos @Idera..was really informative…!

  • Ifiok Moses December 4, 2011, 7:15 am

    Nice post as always.

  • Gulab December 4, 2011, 8:28 am

    It’s been long time Mike published new Exchange blog post on his blog site. But I must say its worth waiting.
    Good work Mike, I needed this….

  • Mike Pfeiffer December 4, 2011, 9:10 am

    Thanks Guys

  • jeremy medard January 9, 2012, 8:42 am

    Hello,

    How would you do that in Exchange 2007 as the syntax and command and different with 2010.

  • Mike Pfeiffer January 9, 2012, 9:29 am

    I haven’t tested this, but I think this will work…

    Import-Csv C:\AddressList.csv | ForEach-Object{
      $name = $_.Name
      $proxy = $_.ProxyAddresses -split ';'
      $Temp = Get-Mailbox -Identity $name
      $Temp.EmailAddresses += $proxy
      Set-Mailbox -Identity $name -EmailAddresses $Temp.EmailAddresses
    }
    
  • bernabe January 26, 2012, 5:04 am

    Thanks a lot! Didn’t know about the -split switch and was going nuts trying to get all to properly format..

  • Damon February 10, 2012, 1:34 pm

    Is there any reason this wouldn’t work against a Distribution Group? I’m trying to run it and I’m getting an error :

    Cannot process argument transformation on parameter ‘EmailAddresses’. Cannot convert value “System.Collections.Hashtable” to type “Microsoft.Exchange.Data.ProxyAddressCollection”. Error: “Failed to convert from System.String to Microsoft.Exchange.Data.ProxyAddress. Error: Error while converting string ” to result type Microsoft.Exchange.Data.ProxyAddress: The e-mail address cannot be empty.”

  • Mike Pfeiffer February 10, 2012, 1:49 pm

    Yeah it will work with Set-DistributionGroup. Since you’re getting an error saying “The e-mail address cannot be empty” are you sure you are referencing the correct column name in the CSV file?

  • josh June 20, 2013, 2:09 pm

    I’m getting the same error when trying on DLs…
    Import-Csv C:\temp\AddressList.csv | ForEach-Object{
    $name = $_.Name
    $proxy = $_.ProxyAddresses -split ‘;’
    $Temp = Get-DistributionGroup -Identity $name
    $Temp.EmailAddresses += $proxy
    Set-DistributionGroup -Identity $name -EmailAddresses $Temp.EmailAddresses
    }

    Exception setting “EmailAddresses”: “Cannot convert value “System.Object[]” to type “Microsoft.Exchange.Data.ProxyAddressCollection”. Error: “The e-mail address cannot be empty.””
    At C:\Temp\DLAddProxy.ps1:5 char:3
    + $Temp.EmailAddresses += $proxy
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

  • Mike Pfeiffer June 21, 2013, 9:25 am

    Is your CSV file setup exactly as shown in the post?

  • Ed October 16, 2012, 5:35 am

    Hi Mike,

    I have been looking for this since a while and this is the faster and easier way to do it. Not sure if the split command works with Office 365, I guess it’s PowerShell V.2. When I run this command I received this error:
    “Unexpected token ‘proxy’ in expression or statement”

    The “Proxy” variable is being declared $proxy = $_.ProxyAddresses -split ‘;’

    Any Ideas? I appreciate your help .

    Cheers.

  • Mike Pfeiffer October 20, 2012, 3:18 pm

    Is your CSV file setup as shown in the post?

  • Markus January 8, 2013, 6:20 am

    Hi Mike,

    i´ll found your script because it would perfectly fit my needs – i´ll have to add several legacy EMailAddresses to existing users but are not able to use Address Policies, due to not sufficient filter criteria.
    Unfortunately the script fails already at step 1 – the demo – if i use it with | foreach{$_.ProxyAddresses -split ‘;’} no output at all.
    If i use it without “foreach{$_.ProxyAddresses -split ‘;’}” the import-csv cmdlet shows me all members of the csv, properly separated with semicolons.

    What is wrong here? We´re Running Exchange 2010 SP2 RU4v2 – ca this be an issue?
    Any help would be greatly appreciated

    BR,
    markus

  • Mike Pfeiffer January 10, 2013, 10:27 pm

    Can you post the error?

  • Joshua Vella July 16, 2013, 7:11 am

    This has worked for Office 365 as well. Thanks, this helped alot!

  • Thomas July 17, 2013, 4:43 am

    Hi,
    I get this error, any ideas?:

    Set-Mailbox : Cannot bind argument to parameter ‘Identity’ because it is null.
    At C:\Scripts\Add ProxyAddress.ps1:7 char:24
    + Set-Mailbox -Identity <<<< $name -EmailAddresses $Temp.EmailAddresses
    + CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Exchange.Management.RecipientTasks.SetMailbox

    Property 'EmailAddresses' cannot be found on this object; make sure it exists and is settable.
    At C:\Scripts\Add ProxyAddress.ps1:6 char:9
    + $Temp. <<<< EmailAddresses += $proxy
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

    Set-Mailbox : Cannot bind argument to parameter 'Identity' because it is null.
    At C:\Scripts\Add ProxyAddress.ps1:7 char:24
    + Set-Mailbox -Identity <<<< $name -EmailAddresses $Temp.EmailAddresses
    + CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Exchange.Management.RecipientTasks.SetMailbox

  • Thomas July 17, 2013, 4:56 am

    ahhh…. worked after adding -Delimiter ‘;’ -Path
    Import-Csv -Delimiter ‘;’ -Path C:\scripts\AddressList.csv| ForEach-Object{

  • JT January 23, 2014, 4:29 am

    Mike, thanks for the simple solution to a tricky problem.

Leave a Comment