Office 365 - Add Alias - contoso.com

Hi Experts,

Trying to add alias addresses to all mailbox accounts so that they can have @contoso.com as an additional mailbox.

I have come up with this script but it does not seem to work:

$Users = Get-Mailbox

Foreach ($User in $Users){

   $ProxyAddresses = (Get-Mailbox $user.alias).emailaddresses
   Foreach ($address in $ProxyAddresses) {
        If (($Address.PrefixString -eq "smtp") -and ($address.AddressString.split("@")[1] `
        -eq "contoso.com")) {
              $ProxyAddresses += $Address
        }
   }
   Set-Mailbox $user.alias -EmailAddresses $ProxyAddresses 
}

Open in new window


Any ideas?
introluxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mahesh SharmaCommented:
Hi

so you basically need to add an additional smtp address for all mailbox with alias@contoso.com address?

Regards
Mahesh
introluxAuthor Commented:
Yes
introluxAuthor Commented:
Also a good alias to also add if possible we would like firstname initial. Surname @domain.com

But not ciritical
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Mahesh SharmaCommented:
Try below:

$Users = Get-Mailbox -resultsize unlimited

Foreach ($User in $Users){

$alias = $user.alias
$newAddress = $alias + "@contoso.com"
Set-Mailbox $alias -EmailAddresses @{Add=$newAddress}

}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mahesh SharmaCommented:
For first name name initial and last name use and use in loop

$a = Get-user $user.alias
$FI = ($a.firstname).substring(0,1)
$LName = $a.LastName
$Email = $FI + $LName + "@contoso.com"
introluxAuthor Commented:
Error on the last script:

Cannot process argument transformation on parameter 'EmailAddresses'. Cannot convert value "System.Collections.Hashtable" to type "Microsoft.Exchange.Data.ProxyAddressCollection". Error: "MultiValuedProperty collections cannot contain null values.
Parameter name: item"
    + CategoryInfo          : InvalidData: (:) [Set-Mailbox], ParameterBindin.   ..mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
introluxAuthor Commented:
Sorry the last error was my fault. here is the correct error:

Cannot process argument transformation on parameter 'Identity'. Cannot convert
value "firstname.surname" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter". Error: "Cannot convert hashtable to an object of the following type:
 Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter. Hashtable-to-Object
 conversion is not supported in restricted language mode or a Data section."
    + CategoryInfo          : InvalidData: (:) [Set-Mailbox], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
introluxAuthor Commented:
Here is the script I am trying to run:

 $Users = Get-Mailbox -resultsize unlimited

 Foreach ($User in $Users){
 
 $a = Get-user $user.alias
 $FI = ($a.firstname).substring(0,1)
 $LName = $a.LastName
 $Email = $FI + $LName + "@contoso.com" 
 Set-Mailbox $a -EmailAddresses @{Add= $Email}
 
 }

Open in new window

introluxAuthor Commented:
Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Office 365

From novice to tech pro — start learning today.