Set-Mailbox proxy addresses

I’m trying to add several proxy addresses to all user accounts on the domain, I can do via exporting the proxy addresses and then importing them. But was wondering if I can do without using csv’s.

But i get “System.Collections.Hashtable” errors

This wont work

$users = Get-ADUser -SearchBase “ou=users,dc=contoso,dc=com” -Filter { (Enabled -eq $true) -and (mail -like “*”) } -Properties displayName
foreach($user in $users){
$name = ($user.DisplayName -split " ")
$firstName = $name[0]
$surname = $name[1]
$addresses = $firstName + “.” + $surname + ‘@email1.com’ + “,” + `
$firstName + “.” + $surname + ‘@email2.com’ + “,” + `
$firstName + “.” + $surname + ‘@email3.com’ + “,” + `
$firstName + “.” + $surname + ‘@email4.com’ + “,” + `
$firstName + “.” + $surname + ‘@email5.com
Set-Mailbox -Identity $name -EmailAddresses @{add= $addresses}
}

This does work

$users = Get-ADUser -SearchBase “ou=users,dc=contoso,dc=com” -Filter { (Enabled -eq $true) -and (mail -like “*”) } -Properties displayName
foreach($user in $users){
$displayname = $user.DisplayName
$name = ($user.DisplayName -split " ")
$firstName = $name[0]
$surname = $name[1]
$addresses = $firstName + “.” + $surname + ‘@email1.com’ + “,” + `
$firstName + “.” + $surname + ‘@email2.com’ + “,” + `
$firstName + “.” + $surname + ‘@email3.com’ + “,” + `
$firstName + “.” + $surname + ‘@email4.com’ + “,” + `
$firstName + “.” + $surname + ‘@email5.com
New-Object -TypeName PSCustomObject -Property @{
Name = $displayname
ProxyAddresses = $addresses
} | Export-Csv -Path c:\temp$displayname.csv
Import-Csv c:\temp$displayname.csv | ForEach-Object{
$name = $.Name
$proxy = $
.ProxyAddresses -split ‘,’
Set-Mailbox -Identity $name -EmailAddresses @{add= $proxy}
}
}

Thanks

As a note, all that string concatenation isn’t strictly necessary. PowerShell replaces variables with their values inside double quotes. Just makes reading the code a little easier.

The help says that -EmailAddresses accepts one object of the type ProxyAddressCollection. You’re providing a single, comma-delimited string as a single value within a hash table.

One way to do this:

$foo = get-mailbox bsuneja
$foo.EmailAddresses += “bsuneja@e14labs.com”,”bharat.suneja@e14labs.com”
$foo | set-mailbox

Notice the comma location in relation to the quotes: It’s OUTSIDE the quotes. So this example is appending two new values to the proper, and then passing the mailbox object to Set-Mailbox. That’s because it’s often easier to get the necessary collection object by just getting the mailbox.

But, if you prefer:

Set-Mailbox bsuneja -EmailAddresses @{Add=‘bharat.suneja@e14labs.com’}

This is closer to what you’re trying to do, except the hash table needs a key (either ‘Add’ or ‘Remove’), with the value being the e-mail address. I imagine this would also be legal:

Set-Mailbox bsuneja -EmailAddresses @{Add=‘bharat.suneja@e14labs.com’;Add=‘poo@lovable.com’}

I’ve given two key/value pairs, here.

Stolen from http://exchangepedia.com/2007/03/how-to-add-additional-email-addresses-to-a-recipient.html.

Don, in this example you make an error:
Set-Mailbox bsuneja -EmailAddresses @{Add=‘bharat.suneja@e14labs.com’;Add=‘poo@lovable.com’}
This leads to double hash key, the right oneliner is
Set-Mailbox bsuneja -EmailAddresses @{Add=‘bharat.suneja@e14labs.com’,‘poo@lovable.com’}

If you want to use variables in your MultiValuedProperty, this is a working syntax:

Set-Mailbox -Identity $user.SamAccountName -EmailAddresses @{'Add' = ($user.GivenName + '.' + $user.Surname + '@domain.com')}

Cant seem to get it working, its the same with removing a single email address

Set-Mailbox $user.DisplayName -EmailAddresses @{‘remove’=($user.mail)}

Still getting a data validation exception