DCSIMG
Managing email addresses in Exchange 2010 - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

Managing email addresses in Exchange 2010

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.

Comments

Twitter Trackbacks for Managing email addresses in Exchange 2010 - Shay Levy [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Managing email addresses in Exchange 2010 - Shay Levy         [microsoft.co.il]        on Topsy.com

# November 22, 2010 12:44 PM

Jan Egil`s blog on Microsoft Infrastructure said:

Adding or removing e-mail addresses from mailboxes using the Exchange Management Shell in Exchange 2007

# November 22, 2010 8:23 PM

Add/remove e-mail addresses using Exchange Management Shell « blog.powershell.no said:

Pingback from  Add/remove e-mail addresses using Exchange Management Shell « blog.powershell.no

# November 22, 2010 8:24 PM

Jan Egil`s blog on Microsoft Infrastructure said:

Adding or removing e-mail addresses from mailboxes using the Exchange Management Shell in Exchange 2007

# November 22, 2010 8:24 PM

Managing email addresses in Exchange 2010–Powershell « MS Tech BLOG said:

Pingback from  Managing email addresses in Exchange 2010–Powershell « MS Tech BLOG

# November 23, 2010 4:51 PM

Managing email addresses in Exchange 2010 – Shay Levy | www.erasedmail.com said:

Pingback from  Managing email addresses in Exchange 2010 – Shay Levy | www.erasedmail.com

# November 24, 2010 8:06 AM

Managing email addresses in Exchange 2010 and 2007 « Ilantz’s Weblog said:

Pingback from  Managing email addresses in Exchange 2010 and 2007 « Ilantz’s Weblog

# November 30, 2010 7:57 PM

ed said:

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".

# December 22, 2010 10:19 PM

ScriptFanatic said:

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

# January 3, 2011 6:52 PM

PST said:

What about Public folders?

The command set-mailpublicfolder? It doesn't work?

Any ideas?

Thx, great post

# March 3, 2011 12:18 PM

Anders' TechNet Blog said:

Adding or removing e-mail addresses from mailboxes using the Exchange Management Shell in Exchange 2007

# March 28, 2011 3:29 PM

Macro said:

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.

# April 29, 2011 12:03 AM

ScriptFanatic said:

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.

# May 1, 2011 7:28 PM

Jason said:

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?

# May 11, 2011 5:42 PM

ScriptFanatic said:

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')}

}

# May 11, 2011 6:17 PM

Greg said:

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?

# May 17, 2011 11:00 PM

ScriptFanatic said:

@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: technet.microsoft.com/.../dd351136.aspx

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

# May 18, 2011 10:35 AM

Greg said:

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

# May 18, 2011 5:01 PM

Dario said:

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?

# May 20, 2011 6:36 AM

ScriptFanatic said:

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.

# May 22, 2011 2:29 PM

Dario said:

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.

# May 22, 2011 10:43 PM

ScriptFanatic said:

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'}

# May 23, 2011 12:05 PM

Dario said:

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

# June 1, 2011 12:05 AM

ScriptFanatic said:

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

# June 5, 2011 1:21 PM

Jeff Horton said:

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

# June 16, 2011 10:54 PM

ScriptFanatic said:

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

# June 16, 2011 11:44 PM

Dan said:

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

# August 16, 2011 10:07 PM

ScriptFanatic said:

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

# August 17, 2011 10:10 AM

Kim said:

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?"

# September 13, 2011 11:06 PM

Blaugrana said:

Great article.

Helped me alot and saved me alot of time!!!!!

# February 22, 2012 9:16 AM

Tariq Jaber said:

Thanks for this information,

is there any way to remove x400 addresses without using the AdModify tool

# April 30, 2012 12:21 PM

ScriptFanatic said:

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

# April 30, 2012 2:30 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: