DCSIMG
How to modify email addresses with PowerShell 2.0 - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

How to modify email addresses with PowerShell 2.0

One of the most asked question across many PowerShell forums, by Exchange administrators, is how to modify email addresses for mailbox users. The most common used code to add new email address in Exchange 2007 is:

PS > $mbx = Get-Mailbox shay

PS > $mbx.EmailAddresses += “newAddress@domain.com”
PS > Set-Mailbox -Identity $mbx –EmailAddresses $mbx.EmailAddresses


With Windows PowerShell 2.0 and Exchange 2007 (and above, sorry guys no 2003 ) we can use the Update-List cmdlet to add,remove or replace email addresses in a middle of a pipeline.

PS > Get-Mailbox shay | Select-Object -ExpandProperty EmailAddresses | `
Where-Object {$_.PrefixString -eq 'smtp'} SmtpAddress : shay@domain.com AddressString : shay@domain.com ProxyAddressString : SMTP:shay@domain.com Prefix : SMTP IsPrimaryAddress : True PrefixString : SMTP

 

We see that my mailbox has only one email address. Lets try to add two more addresses and then get the email addresses collection back:

PS > Get-Mailbox shay | `
Update-List -Property EmailAddresses -Add ScriptFanatic@domain.com,ShayL@domain.com | `
Set-Mailbox


PS > Get-Mailbox shay | Select-Object -ExpandProperty EmailAddresses | `
Where-Object {$_.PrefixString -eq 'smtp'} SmtpAddress : ShayL@domain.com AddressString : ShayL@domain.com ProxyAddressString : smtp:ShayL@domain.com Prefix : SMTP IsPrimaryAddress : False PrefixString : smtp SmtpAddress : ScriptFanatic@domain.com AddressString : ScriptFanatic@domain.com ProxyAddressString : smtp:ScriptFanatic@domain.com Prefix : SMTP IsPrimaryAddress : False PrefixString : smtp SmtpAddress : shay@domain.com AddressString : shay@domain.com ProxyAddressString : SMTP:shay@domain.com Prefix : SMTP IsPrimaryAddress : True PrefixString : SMTP

 

UPDATE [11/22/2010]: The method described below no longer works in Exchange 2010 and Exchange 2007 SP2. Check this post for more information.

Notice that the primary email address is indicated by the ProxyAddressString property (True) and the ProxyAddressString property which starts the with an uppercased prefix of ‘SMTP:’ . To remove email addresse(s) we use the –Remove parameter:

PS > Get-Mailbox shay | ` 
     Update-List -Property EmailAddresses –Remove ScriptFanatic@domain.com,ShayL@domain.com | `
     Set-Mailbox

 

 

The Add and Remove parameters can be used together in a pipeline, however we cannot remove the primary address:

PS > Get-Mailbox shay | `
Update-List -Property EmailAddresses -Remove shay@domain.com | Set-Mailbox Update-List : Cannot remove the primary SMTP address 'shay@domain.com'.
Please promote another valid SMTP address before removing this one.

 

But we can set a new email address as the primary address (and remove the old one later), just add the ‘SMTP:’ prefix.

PS > Get-Mailbox shay | Update-List -Property EmailAddresses -Add "SMTP:PSFanatic@domain.com" | Set-Mailbox

 

From the Update-List cmdlet help description:

“This cmdlet works only when the property that is being updated supports the IList interface that Update-List uses. Also, any Set-* cmdlets that accept an update must support the IList interface. The core cmdlets that are installed with Windows PowerShell do not support this interface. To determine whether a cmdlet supports Update-List, see the cmdlet Help topic.”

 

So, what other properties support the IList interface? With the following command you can get a list of mailbox properties that can be modified with Update-List.

PS > $mbx = Get-Mailbox shay 
PS > $mbx.GetType().GetProperties() | `
Where-Object {$_.PropertyType.GetInterface("System.Collections.IList",$true)} | `
Select-Object Name Name ---- Languages ProtocolSettings ResourceCustom Extensions AcceptMessagesOnlyFrom AcceptMessagesOnlyFromDLMembers AddressListMembership EmailAddresses GrantSendOnBehalfTo PoliciesIncluded PoliciesExcluded RejectMessagesFrom RejectMessagesFromDLMembers UMDtmfMap ObjectClass

 

Finally, there is also a –Replace parameter we can use to replace all existing email addresses with new ones. For more information see the code examples for Update-List.

Comments

Twitter Trackbacks for How to modify email addresses with PowerShell 2.0 - Shay Levy [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 How to modify email addresses with PowerShell 2.0 - Shay Levy         [microsoft.co.il]        on Topsy.com

# January 21, 2010 2:58 PM

Israel Lopez Cabello said:

Hello man.

Your post is very interesting.

I am new in Power Shell tools, and Actully I am creating new users by using power shell. I have a script that I would like to use, but there are something that is missing to be compleated. I hope you can help me.

$Nombre = "domain.com/Users/Thom Dietz"

$Alien = "tdietz"

$Direcc = "thom.dietz"

Enable-Mailbox -Identity $Nombre -Alias $Alien -Database "ClusterMailBox\SG1\DB1"

set-mailbox -identity $Alien -emailaddresspolicyenable $false -primarysmtpaddress $Direcc  

Note1: I would like to add "@domain1.com" to $Direcc variable in this part. But I don't know how to do that

$Temp = Get-Mailbox -Identity $Alien

$Temp.EmailAddresses.Add("smtp:_____________________")

Set-Mailbox -Instance $Temp

Note2:How could I use $Direcc variable plus "@domain2.com" in this part?

# February 8, 2010 5:45 PM

ScriptFanatic said:

Try this:

$mbx = Get-Mailbox $Alien

$mbx.EmailAddresses | Where {$_ -eq $Direcc} | Foreach {$_ = "$_$Direcc"}

Set-Mailbox -Identity $mbx –EmailAddresses $mbx.EmailAddresses

# February 12, 2010 4:45 PM

Jose said:

Hi!!

Very interesting post!.

Does anyone know how to do this in Exchange Online?

I'm lookin for information about how to modify smtp address with cmdlets in Exchange Online, but i'm starting to think right now is not posible...

Thanks!

# February 22, 2010 5:29 PM

ScriptFanatic said:

I have never played with Exchange Online so I'm not sure how to answer, maybe in the same way as you would do with Exchnage on-premise?

Do you have access to Set-Mailbox or Update-List?

# February 22, 2010 6:43 PM

sys admin said:

I'm new  to powershell.  I'm trying to remove all email addresses except the primary one.  Here's where I get stuck:

$mailbox= Get-Mailbox USER

Update-List -inputobject $mailbox -Property EmailAddresses -Remove everything except primary smtp| set-mailbox

whats the syntax for 'everything but primary smtp'

# April 27, 2010 5:12 AM

ScriptFanatic said:

There's no special syntax to remove all email addresses except the primary one. Try this:

# get the mailbox

$mbx = Get-Mailbox test

# get all non primary addresses

$emails = $mbx.EmailAddresses | where {!$_.IsPrimaryAddress}

# remove non primary addresses and update the mailbox object

$mbx | Update-List -Property EmailAddresses -Remove $emails | Set-Mailbox

# April 27, 2010 10:13 AM

sys admin said:

worked perfectly...thank you.

# April 27, 2010 8:03 PM

psnewbie said:

I have a slightly different task to accomplish. I have a list of users who need a new email address. This needs to be done in a specific format:

firstname_lastname_temp@exchange.com

They already have something like firstname_lastname@exchange.com already set as their primary address thru a recipient policy.

Any ideas?

# June 15, 2010 2:52 PM

ScriptFanatic said:

You can modify the address to the pattern you need but next time the policy runs it will add another email address (based on the policy) and make the new address the primary one.

One way to avoid that is to turn off policy changes on the object level or create (and apply) another email policy based on some criteria (user attribute). You can find an example here:

www.exchangeninjas.com/New-EmailAddressPolicy

# June 15, 2010 3:24 PM

Tommy said:

The Update-List cmdlet does not appear to work against E2010 mailboxes.

Get-Mailbox tester | Update-List -Property EmailAddresses -Add tester@domain.com | Set-Mailbox

I try exactly the same line against an E2007 mailbox and it works as expected.

Is there a solution for E2010 mailboxes?

# August 23, 2010 4:47 AM

Gonza said:

Hi

I just worked with info on this page and found that Update-List is not working in Ex 2010 enviroment. It drops me: WARNING: The command completed successfully but no settings of 'bla.NET/BMatjaz Gonza' have been

modified.

I used working code :) :

PS > $mbx = Get-Mailbox shay

PS > $mbx.EmailAddresses += “newAddress@domain.com”

PS > Set-Mailbox -Identity $mbx –EmailAddresses $mbx.EmailAddresses

# November 8, 2010 1:02 PM

Jan Egil Ring said:

The following works in Exchange 2010:

Get-Mailbox -Resultsize unlimited | ForEach-Object {

$emailaddresses = $_.emailaddresses

$remove = $emailaddresses | Where-Object {-not $_.IsPrimaryAddress -and $_.smtpaddress -like "*@123.com"}

if ($remove) {

foreach ($smtpaddress in $remove) {

$_ | Update-List -Property emailaddresses -Remove $smtpaddress.smtpaddress

}

$_ | Set-Mailbox -emailaddresses $emailaddresses

}

}

# November 11, 2010 6:00 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 14, 2010 1:51 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 14, 2010 2:01 PM

ScriptFanatic said:

Gonza, This method no longer works in Exchange 2010 (2007 Sp2). See this post for more information:

blogs.microsoft.co.il/.../Managing-email-addresses-in-exchange-2010.aspx

# November 22, 2010 12:09 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

Zhivko Tashev said:

The greatest article I have come across in my time. 2 days manual work done in 15 minutes waiting.

Thank you

# December 22, 2011 3:53 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: