Warning: Undefined array key "oxygen-cache-1409" in /var/www/wordpress_jb/wp-content/plugins/jetpack/class.jetpack.php on line 5759

Warning: Undefined array key "oxygen-cache-1414" in /var/www/wordpress_jb/wp-content/plugins/jetpack/class.jetpack.php on line 5759

Warning: Undefined array key "oxygen-universal-styles" in /var/www/wordpress_jb/wp-content/plugins/jetpack/class.jetpack.php on line 5759

Using Powershell To Generate Test Mailboxes

Posted on August 2, 2008

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:

UPN,FirstName,LastName,Password
bruth,Babe,Ruth,[email protected]!
mmantle,Mickey,Mantle,[email protected]!
hwagner,Honus,Wagner,[email protected]!
brobinson,Brooks,Robinson,[email protected]!
yberra,Yogi,Berra,[email protected]!
kpuckett,Kirby,Puckett,[email protected]!
rcarew,Rod,Carew,[email protected]!
hkillebrew,Harmon,Killebrew,[email protected]!
twilliams,Ted,Williams,[email protected]!
jdimaggio,Joe,DiMaggio,[email protected]!

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.

One comment on “Using Powershell To Generate Test Mailboxes”

About JB

I've been a technologist for over 20 years, getting my start in IT with a large Fortune 100 international retailer in the 1990s. I love to talk about new technology, mobile devices, software development, photography, and the weather.  The opinions expressed on this blog are my own.

Follow me

Copyright © Justin Braun.  All rights reserved.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
%d bloggers like this: