Creating Multiple Mailboxes in the Exchange Management Shell
Given
the complexity of creating a new mailbox in the EMS, why would anyone
want to do so? Generally, they wouldn’t. But what if your Human
Resources Department handed you a list of 50 new employees and
requested that you create new mailboxes for all of them? Doing so
through the GUI interface of the EAC would not only take hours, but
would also result in the increased likelihood that misspellings or
mistakes might occur.
That’s where the power of the EMS comes into play.
By putting the list of names in a .csv
file, you can quickly create multiple accounts from only two lines of code. To do so, perform the following steps:
1. Create a text file called newusers.csv
in a directory called (for our example) c:\scripts
.
2. For this example, create several column names and populate the data, as shown in Figure 4. The columns to populate are Name, Alias, UPN, First, and Last. Additional column names can be added, if desired, to populate more data in the user accounts.
Figure 4. Creating the CSV file for multiple mailbox creation.
3. When the .csv
file is complete, you are ready to begin. Each user account will be
created with a default password, with the user required to reset the
password when he or she first logs in. From the Exchange Management
Shell, type the following command. (Type it word for word, do not
attempt to enter the password yet.)
$Password = Read-Host "Enter Password" -AsSecureString
Press
Enter. You will be presented with a prompt stating Enter Password. Type
the password you want to apply to all your newly created users and
press Enter. Your password will now be assigned to the variable $Password
for use in your script.
4. Next, you run two cmdlets, piping the results of the first into the second, to create the new mailboxes from the .csv
file. The syntax will be as follows:
Import-Csv "c:\scripts\newusers.csv" | foreach { New-Mailbox –name $_.Name
–alias $_.Alias –UserPrincipalName $_.UPN -FirstName $_.First -LastName
$_.Last -Password $Password
–ResetPasswordOnNextLogon:$true}
The result, as shown in Figure 5,
is the creation of the new mailboxes. The existence can be confirmed by
viewing the mailboxes in the Exchange Management Console. (Remember to
refresh the screen if you already had it open.)
Figure 5. End result of multiple mailbox creation script.
Again—while
this is a significant amount of work for three users, the same concept
can be used to create 50 users (or 500) and can prove to be a valuable
time-saver.