Managing email addresses in Exchange 2010

November 22, 2010

31 comments

In this post I wrote about a way to modify email addresses with the Update-List cmdlet in a Exchange 2007 environment. Exchange 2010 has been released in the meantime and this method no longer works, you’ll get this warning:

[PS] > Get-Mailbox User1 | Update-List -Property EmailAddresses -Add TestUser1@domain.com | Set-Mailbox
WARNING: The command completed successfully but no settings of ‘domain.com/Users/TestUser1’ have been modified.

 

Exchange 2010 uses PowerShell 2.0 Remote PowerShell sessions to manage Exchange and that changed the way objects and parameters are bind in the implementation of remote Exchange commands.

In 2010 we can use a hash table to modify the EmailAddresses property. For example, to add new SMTP address we use the ‘Add’ keyword:

[PS] > Set-Mailbox User1 -EmailAddresses @{Add=’TestUser1@domain.com’}

The ‘Add’ keyword can be replaced with the ‘+’ shortcut:

[PS]  > Set-Mailbox User1 -EmailAddresses @{‘+’=’TestUser1@domain.com’}

 

To remove addresses use the ‘Remove’ keyword, or its ‘-‘ alias.

[PS]  > Set-Mailbox User1 -EmailAddresses @{Remove=’TestUser1@domain.com’}

[PS]  > Set-Mailbox User1 -EmailAddresses @{‘-‘=’TestUser1@domain.com’}

 

Note that if you decide to use the +/- aliases then you need to enclose them in quotes otherwise PowerShell will interpret them as the ‘-=’/’+=’ PowerShell operators.

We can also add and remove addresses in one call:

[PS] >  Set-Mailbox User1 -EmailAddresses @{‘Add’=@(‘TestUser20@domain.com’,’TestUser21@domain.com’); Remove=’TestUser1@domain.com’}

 

Now, the EmailAddresses parameter is just one example, in fact you can use this method to modify any other MultiValuedProperty parameter:

[PS]  > Set-RecipientFilterConfig -BlockedRecipients @{Add=@(‘user1@domain.com’,’user2@domain.com’)}

[PS]  > Set-TransportConfig -GenerateCopyOfDSNFor @{Add=@(‘5.4.7’, ‘5.7.1’);Remove= ‘5.7.2’}

[PS]  > Get-Mailbox User2 | Set-Mailbox -RejectMessagesFrom @{Add=’administrator@contoso.com’; Remove=’sysadmin@contoso.com’}

[PS]  > Get-Mailbox User2 | Set-Mailbox -GrantSendOnBehalfTo @{Add=(‘administrator@contoso.com’,’sysadmin@contoso.com’)} 
   

 

Have fun.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

31 comments

  1. edDecember 22, 2010 ב 22:19

    Getting this error on E2K7 SP3:

    Set-Mailbox : Cannot bind parameter ‘EmailAddresses’. Cannot convert the “System.Collections.Hashtable” value of type “System.Collections.Hashtable” to type ”
    Microsoft.Exchange.Data.ProxyAddress”.

    Reply
  2. ScriptFanaticJanuary 3, 2011 ב 18:52

    Ed, looks like it’s a 2010 feature. It doesn’t work for me as well in 2007 SP3.

    Reply
  3. PSTMarch 3, 2011 ב 12:18

    What about Public folders?
    The command set-mailpublicfolder? It doesn’t work?

    Any ideas?

    Thx, great post

    Reply
  4. MacroApril 29, 2011 ב 00:03

    Is it possible to use wildcards in the Domain Name hashtable? So each user has different formats and I would just like to remove any address with @domain.com.

    I tried using *@domain.com but it gives me the warning that nothing was modified.

    Reply
  5. ScriptFanaticMay 1, 2011 ב 19:28

    I don’t think it works with wildcards, in my opinion it takes the asterisk literally (no such address) and that’s why you get the message that nothing was modified.

    Reply
  6. JasonMay 11, 2011 ב 17:42

    I am a little new to this, but I have been dabbling. Was wondering if you can help me figure out where my thinking is wrong here…

    Get-Mailbox |Where {$_.CustomAttribute1 -eq ‘Internal’} | Set-Mailbox -EmailAddresses @{‘-‘=($_.Alias+’@contoso.com’)}

    It is coming back with command completed successfully but no settings modified. My guess is the $_.Alias+’@contoso.com’ is not correctly changing over to the users email address that I want to remove. Is something like this possible?

    Reply
  7. ScriptFanaticMay 11, 2011 ב 18:17

    Two things:

    First, use server side filtering whenever you can, using the -Filter parameter, instead of the Where-Object cmdlet. The first performs much faster.

    Second, you’re piping mailbox objects directly to the Set-Mailbox cmdlet but you use the $_ (current object) which is unavailable (null), $_ “lives” only inside script blocks (where-object, foreach-object are example cmdlets).
    To make it work, pipe the result to the foreach-object cmdlet and then use the $_.Alias member:

    Get-Mailbox -Filter {CustomAttribute1 -eq ‘Internal’} | Foreach-Object{
    Set-Mailbox -Identity $_ -EmailAddresses @{‘-‘=($_.Alias+’@contoso.com’)}
    }

    Reply
  8. GregMay 17, 2011 ב 23:00

    ScriptFanatic, your script is a good idea, but doesn’t work in the Exchange console. I get an error:
    “Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.”

    According to my research, this is becuase of the foreach-object command which we are not allowed to use with set-mailbox. I’ve tried serveral ways around this but to no avail. Any ideas?

    Reply
  9. ScriptFanaticMay 18, 2011 ב 10:35

    @Greg

    PowerShell remoting do not support more than one pipeline running at the same time. First, save the mailboxes you query to a variable and then pipe that variable to the foreach-object cmdlet. This will execute one pipeline at a time for each mailbox object.

    $mbx = Get-Mailbox …
    $mbx | Foreach-Object {…}

    The issue is also mentioned here: http://technet.microsoft.com/en-us/library/dd351136.aspx

    Expand ‘Client Issues/Error When the ForEach Cmdlet Is Used in a Pipeline and Cmdlets Are Used in its Script Block’

    Reply
  10. GregMay 18, 2011 ב 17:01

    What a simple solution. Thanks ScriptFanatic, this worked like a charm!

    Reply
  11. DarioMay 20, 2011 ב 06:36

    Hi ScriptFanatic,

    I’m hoping you can help as it kind of relates to this page.

    I’m fairly new to powershell.

    I’m getting incorrect smtp entries added to a mailbox when the mailbox is created with same information as a contact which already exists.

    If i open the mailbox manually and edit this “rogue” smtp entry i can change it to the correct one no problem.

    Heres a brief description of what im trying to do.

    Domain: contoso.com
    user: bobmarley
    primary smtp: bmar001@abc.contoso.com
    “rogue smtp” bobmarley2@contoso.com (the 2 is the problem in question)
    what it should be: bobmarley@contoso.com

    If i manually (by going through the gui) take the 2 out then its all fine.

    What i’m trying to do is remove that entry and replace it with a correct one automatically in powershell.

    Any ideas?

    Reply
  12. ScriptFanaticMay 22, 2011 ב 14:29

    Do you have an email address policy in your Exchange environment? Looks like that one of your objects already owns the bobmarley@contoso.com address and the policy adds “2” to disambiguate it.

    Reply
  13. DarioMay 22, 2011 ב 22:43

    Thats what i thought however if i delete the 2 manually, i get no errors and everything is fine.

    If an object already owned bobmarley@contoso.com, i wouldnt be able to change it manually surely.

    Reply
  14. ScriptFanaticMay 23, 2011 ב 12:05

    As long as the address is not assigned to another object you can do whatever you want with it.

    Back to your original question, Have you tried the following command?

    Set-Mailbox bobmarley -EmailAddresses @{‘Add’=’bobmarley@contoso.com’;  Remove=’bobmarley2@contoso.com’}

    Reply
  15. DarioJune 1, 2011 ב 00:05

    Thanks for that,

    If i run that command it works no problem.

    What im trying to achieve is something lie this:

    $FN = Read-Host “First Name”
    $LN = Read-Host “Last Name”
    $Alias = Read-Host “Alias”
    $Email = Read-Host “Email Address”

    New-Mailbox *** all the parameters for new mailbox ***

    Set-Mailbox $alias -EmailAddresses @{‘Add’=’$email’; Remove=’*@contoso.com’}

    Something similar to that. Basically taking input which is already stored in variables and using that to add/remove SMTP entries.

    What i’m having trouble is, the remove *@contoso.com part, runs but doesnt remove any @contoso addresses

    Reply
  16. ScriptFanaticJune 5, 2011 ב 13:21

    The Add/Remove keys doesn’t support wilcards, you’ll have to go through all addresses and decide if to Add/Remove them.

    Reply
  17. Jeff HortonJune 16, 2011 ב 22:54

    Thanks, great article.

    I used the method below to update just the PrimarySMTPAddress if they had more than 1 address in Exchange 2007:

    $Proxies = Get-Mailbox ‘Jeff.Horton@acme.com’
    $Proxies.EmailAddresses+=’SMTP:Jeff.Horton@acme.anvil.com’
    $Proxies | Set-Mailbox

    How would I do this in 2010 if I only want to update the PrimarySMTPAddress?

    Thanks,
    Jeff

    Reply
  18. ScriptFanaticJune 16, 2011 ב 23:44

    It’s easy 🙂

    You may need to disable the email address policy if the mailbox is configured to use an email address policy and the new address doesn’t match the policy.

    Get-Mailbox Jeff.Horton@acme.com | Set-Mailbox -PrimarySmtpAddress Jeff.Horton@acme.anvil.com -EmailAddressPolicyEnabled $false

    Reply
  19. DanAugust 16, 2011 ב 22:07

    How would you go about removing all legacy addresses? I would like to remove all CCMAIL or MSMAIL addresses that were brought over as the result of a migration. Is there an easy way to script?

    Dan

    Reply
  20. ScriptFanaticAugust 17, 2011 ב 10:10

    @dan, send me a messagel through the Contact at the top of the page.

    Reply
  21. KimSeptember 13, 2011 ב 23:06

    Hi – I have the same question as Dan:

    “How would you go about removing all legacy addresses? I would like to remove all CCMAIL or MSMAIL addresses that were brought over as the result of a migration. Is there an easy way to script?”

    Reply
  22. BlaugranaFebruary 22, 2012 ב 09:16

    Great article.
    Helped me alot and saved me alot of time!!!!!

    Reply
  23. Tariq JaberApril 30, 2012 ב 12:21

    Thanks for this information,
    is there any way to remove x400 addresses without using the AdModify tool

    Reply
  24. ScriptFanaticApril 30, 2012 ב 14:30

    Yes, here’s an example for a single mailbox:

    # get the mailbox
    $mbx = Get-Mailbox TariqJ

    # get all addresses except for x400
    $EmailAddresses=$mbx.EmailAddresses | Where-Object {$_.PrefixString -ne ‘X400’}

    # set the addresses back on the mailbox
    Set-Mailbox $mbx -EmailAddresses $EmailAddresses

    Reply
  25. shaikSeptember 25, 2012 ב 14:37

    I need to remove additional email addresses for users in a specific OU, exchange 2010 sp1 what should be the powershell line.

    Thanks

    Reply
  26. ScriptFanaticSeptember 26, 2012 ב 00:46

    @shaik, what’s the criteria you want to use?

    Reply
  27. samJanuary 16, 2013 ב 18:04

    how to remove trailing spaces in an PrimaySMTPAddress
    I have primarySMTPAddress set as ‘ABC@contoso.com ‘
    – If you notice there is a space in ‘ABC@contoso.com ‘
    – It should be like ‘ABC@contoso.com’
    – I have tons of contacts like this created using
    third party Identity tools. ‘No policy’
    – how to fix the trailing spaces in SMTP addresses

    Reply
  28. RafiFebruary 8, 2013 ב 08:09

    Hi,
    I needs help please with removing all secondary ‘smtp’ addresses (only smtp, not SIP and etc’) except for the Primary SMTP (the EX policy is FirstNmae.LastName@domain.com – for the primary SMTP)
    I have CSV file contains List of 600 Users Display Names.

    Thanks
    A lot.

    Reply
  29. PurishMarch 21, 2013 ב 04:34

    how to rectify this error?

    Set-MailContact -Identity $uniqueKey -$attrKey $attrVal;

    In attrKey variable I’m getting Displayname. While running this commnad I’m getting error like
    A positional parameter cannot be found that accepts argument ‘-DisplayName’.
    + CategoryInfo : InvalidArgument: (:) [Set-MailContact], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Set-MailContact

    Reply
  30. BeebeJune 19, 2013 ב 09:13

    There is certainly a lot to learn about this issue.
    I love all the points you made.

    Reply