Share This Post

PowerShell Script to Create Multiple Exchange Mailbox and Set Primary SMTP Addresses

I am working on a request to create ~800 user’s Exchange 2007 SP1 mailboxes from an excel file.

The file with the following headers filled with user’s information.

Name, FirstName, MI, LastName, Email, OU

The Alias will be in firstname.mi.lastname if there is a MI or otherwise in firstname.lastname format.

I come up the following script:

$Password=Read-Host "Enter Password" -AsSecureString

Import-CSV c:\users.csv | foreach {new-ma
ilbox -firstname $_.firstname -initials $_.MI -Lastname $_.lastname -alias ($_.f
irstname + "." + $_.MI + "." + $_.lastname) -name $_.name -userPrincipalName $_.
email -database "server1\storage group" -org $_.OU -Password $Password -Reset
PasswordOnNextLogon $True}

If the MI column is blank, just removed $_.MI part from the script because Exchange doesn’t understand the firstname..lastname!

I thought that would be it, right? Not quite!

It turns out we have a default policy in place which will automatically change the email address format to first initial + lastname (yli instead ying.li)

So I modified the script as follows:

Import-CSV c:\users.csv | foreach {new-ma
ilbox -firstname $_.firstname -initials $_.MI -Lastname $_.lastname -alias ($_.f
irstname + "." + $_.MI + "." + $_.lastname) -name $_.name -userPrincipalName $_.
email -database "server1\storage group" -org $_.OU -Password $Password -Reset
PasswordOnNextLogon $True}|set-mailbox -emailaddresspolicyenabled $false

which will check off the "Automatically update e-mail addresses based on e-mail address policy" box in user’s mailbox property (email address page), this will prevent the newly created user from inherit the default policy.

I checked the results – the email address already re-formated to yli instead ying.li before the check off policy taking place!

I end up have to run the below script against the excel sheet to reset the user’s primary smtp address to ying.li@whatever.com

Import-CSV c:\users.csv | foreach {$user
= get-mailbox -id ($_.firstname + "." + $_.MI + "." + $_.lastname);$user.emailad
dresses.add($_.email);set-mailbox -instance $user -primarysmtpaddress $_.email}

Now it is all happy!

Share This Post

Ying Li obtained a Master degree of Medicine in China and worked in the medical field before he came to the states in 1996. He made the career switch from troubleshooting people to troubleshooting computers after he came to the states. Not entirely a surprise there, if you know that his second preference for college is Mathematics. Ying is two times Admin Framework MVP awardee (2007-2008).His previous employment experience in the US includes UUNET, MCI WorldCom. MMC (Marsh & McLennan Companies), Merrill Lynch. He is currently employed by Related Companies as a Senior Infrastructure Engineer. He is proficient in virtually all Microsoft products (Office/Back office). His craving for efficiency naturally leads him to scripting, automation and before you know it, PowerShell! He is currently a MCSE (NT4, 2000, 2003) and MCITP Server Administrator. Ying enjoys wines and love to travel. His non work related passion is to advocate Health Living!

Leave a Reply