Using Powershell To Generate Test Mailboxes

Share on facebook
Share on twitter
Share on linkedin

There are situations where you may want to generate a number of test mailboxes whether it be for a demo or another scenario.  With Exchange 2007, you can leverage Powershell cmdlets to complete this process for you in just seconds.

First, I start out with the a CSV file that contains a limited number of columns, basically the minimum needed to create an Active Directory user account and mailbox-enable it.  Since I have a passion for baseball, my test CSV file is a tribute to some of my favorite players in history.  This is what the sample users.csv file looks like:


Next, it only takes a couple lines of code for us to generate the accounts and mailboxes.  You can take this code and save it into a file named something like createmailboxes.ps1.  This is what the script should look like:

# Test Mailbox Creation Script

# Variables Used Globally
$database = “First Storage GroupMailbox Database”
$ou = “Users”
$upnsuffix = “@e2k7test.local”

import-csv users.csv | foreach {$pass = ConvertTo-SecureString $_.Password -AsPlainText -Force; New-Mailbox -Name ($_.FirstName+” “+$_.LastName) -Password $pass -UserPrincipalName ($_.UPN+$upnsuffix) -Database $database -OrganizationalUnit $ou -FirstName $_.FirstName -LastName $_.LastName -DisplayName ($_.FirstName+” “+$_.LastName)}

In my script I include some variables that I reuse for all of my users.  In this case, they are all going to be part of the same organizational unit (OU) in Active Directory and their mailboxes will all be part of the same mailbox database.  They are also part of the same domain so the suffix for their User Principal Name (UPN) will be the same.

Literally, the meat of the script is just one line of code thanks for the piping you can do in Powershell.

The import-csv cmdlet takes in a comma-separated text file and lets us read its contents like a data object.  Basically we’re taking the contents of the CSV file and passing it into the next segment of code which is the “For Each” block.

Since we want to create an account and mailbox for each line in the CSV file, we’ll do that line by line processing within the “For Each” block.  The first command within that block is where we’ll save the password as a variable.  Since the password can’t be passed into the cmdlet as plain text, we have to convert it to a secure string using the ConvertTo-SecureString cmdlet.  We pass in the plain text password and it returns a secure string that can be used.

Next we call the New-Mailbox cmdlet which will create the account and mailbox enable it.  We’ll pass in information like the name of the account, first name, last name, display name, UPN, mailbox database, OU, and password.  There are additional fields that you can include, but are not required.

We can access the different “fields” from the CSV by calling them with the $_.ColumnName format as you can see in the script sample. 

This clearly simplifies the process of bulk account creation and provisioning for Exchange.  We’ll work on continuing to evolve this into a more complex script that adds some additional functionality.

More to explore...

#WindowsUglySweater is back!

It’s back again for the holiday season, and this year it shows-off everybody’s favorite 8-bit drawing app – MS Paint! The previous

One Response

Leave a Reply

Your email address will not be published.