Exchange Server 2007: Bulk creation of mailboxes using Exchange Management Shell

by Bharat Suneja on November 1, 2006

Bulk creation of mailboxes (and the accompanying user accounts) in Exchange Server 2003/2000 involved some elaborate scripting effort. This task can now be done fairly effortlessly, thanks to the Exchange Management Shell (EMS). The Shell can very easily import/use CSV files saved from a spreadsheet or text editor and create the mailbox-enabled users. Here’s how.

Step 1 Create a spreadsheet

Create a spreadsheet (you can also use a standard text editor such as Notepad to create a CSV file) with the following columns. You can add more fields if you wish:

Alias,Name,UPN

Add data for new accounts in following lines/rows. Here’s what it’ll look like:

Alias,Name,UPN
User_One,User One,userone@yourUPNsuffix.com
User_Two,User Two,usertwo@yourUPNsuffix.com
User_Three,User Three,userthree@yourUPNsuffix.com

Save the file as CSV – let’s call it CreateRecipients.csv

Step 2 Enter a password

Now fire up the Shell (EMS) and issue the following command:

$Password=Read-Host “Enter Password” -AsSecureString

This will prompt you for a password. The same password will be used as the initial password for all your mailbox-enabled users.

Step 3 Import the CSV and create mailboxes

Next, issue the following command to import the CSV file and create the user accounts and mailboxes:

Import-CSV CreateRecipients.csv | ForEach {New-Mailbox -Alias $_.alias -Name $_.name -userPrincipalName $_.UPN -Database “Mailbox Database” -OrganizationalUnit Users -Password $Password}

How the Shell interprets the command

Here’s how the shell interprets the above command:

  • Import-csv CreateRecipeints.csv gets data from the CSV file.

    If you’re providing the complete path with spaces, you must enclose it in quotes. For example: “c:\My Scripts\CreateRecipients.csv”.
    - If you’re not running the command from the same directory (folder) where the CSV file is located, you will need to provide the complete path to the file.

  • - The | pipes the data from the CSV to the next command.
  • - foreach parses through each line in your CSV file, using the first row as a header. All the column headings/names get treated as field names.
  • - The New-Mailbox command, as you may already know by now, creates a new mailbox-enabled user.
  • - For each variable, you either enter a value at the command line (for instance, we’re using the value “Mailbox Database” for the Store), or you can pick up the value from your CSV file provided you have a column for it with a heading. For instance, for the -alias field, we use $_.alias – which tells the script to look for the column called alias in your CSV file and pick up the value from the current row or line.

    If you’re running the command on the same server where you want to create the mailboxes, you can simply use the Store name, e.g. “Mailbox Store”. To create mailboxes on another server’s Store(s), you can use either ServerName\Store, or ServerName\StorageGroup\Store, or even the GUID of the Store. How do you get those??? Use the Get-MailboxDatabase cmdlet.

  • - The -OrganizationalUnit field stands for (Active Directory) OU (or Container) you want to create the accounts in. If all the accounts you create from the CSV file will reside in the same container or OU, you can provide this at the command-line, as we did in the example above. Alternatively, you can include the OU/Container name in your spreadsheet/CSV file in a field – let’s say OU – and modify your command to say: -org $_.OU
    Note: Luckily you do not need to provide the distinguishedName of the OU or Container – simply the name of the OU or Container works.
  • - The -password field picks up the password from the variable called $password that we created earlier.

The New-Mailbox cmdlet allows you to enter a lot of attributes for each mailbox-enabled user. These are documented in the New-Mailbox cmdlet reference docs (Exchange 2007 | Exchange 2010), and also in the Exchange 2007/2010 online help. For each attribute you want to populate, add it to your spreadsheet/CSV file.

Updates

  • July 9, 2007: To create new mailboxes with settings based on an existing mailbox, use the TemplateInstance parameter, as shown in the following commands:

    $Template = Get-Mailbox “Template Mailbox”
    Import-CSV CreateRecipients.csv | foreach {new-mailbox -alias $_.alias -name $_.name -userPrincipalName $_.UPN -database “Mailbox Database” -org Users -Password $Password -templateinstance $Template}

  • Sep. 15, 2010: The TemplateInstance parameter is not supported in Exchange 2010

Previous post:

Next post: