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

by Bharat Suneja

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,[email protected]
User_Two,User Two,[email protected]
User_Three,User Three,[email protected]

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

{ 31 comments… read them below or add one }

Anonymous December 8, 2006 at 10:42 am

How would you create bulk mailboxes for users already in the active directory?

Reply

Anonymous January 30, 2007 at 6:28 am

Good work!! please keep it up. And I had the same question as the previous poster….how to mail-enable my already existing users? I guess Exchange 2003 did this automatically?? :-)

Reply

Bharat Suneja January 30, 2007 at 6:42 am

Blogged about mail-enabling existing users in response to the first comment – read post titled ” Exchange Server 2007: Bulk mailbox-enabling users using Exchange Shell” (http://www.exchangepedia.com/blog/2006/12/id-written-about-how-to-bulk-create.html)

Reply

Louis Göhl March 7, 2007 at 4:23 am

Great Blog!

Is it also possible to put more than one alias address in the script?

Reply

Bharat Suneja March 7, 2007 at 9:18 am

Louis,

The alias is the mailNickname attribute – it’s not a multi-valued attribute, so it cannot hold more than one values.

Did you want to add additional email-addresses?

Bharat

Reply

Anonymous May 20, 2007 at 6:18 am

Worked beautifully. Thanks so much for the intro to PowerShell that I needed!

Reply

David May 31, 2007 at 1:49 am

What other AD attibutes can be added here? I’d like to add a custom attribute (staff ID number). Is there a list of attributes that can can be added to the csv file and imported with each user? Thanks so much.

Reply

Bharat Suneja May 31, 2007 at 9:17 am

David,

The last paragraph in the post has a link to Microsoft’s online documentation for new-mailbox command, which lists all the parameters that you can add. The list includes custom attributes as well as plenty of others.

Bharat

Reply

...Shirish... March 25, 2008 at 3:54 am

Thanks Bharat, this blog gives too much info regarding the bulk Ids, and now Im using the script to run (ps1),
I have query here, in the Company field I would like to put the employee number when I start using the script, due to this, most of the time these IDs will show the “INA” and users never update the directory and when we do a random check on the servers these ids will be inactive and we will delete those Inactive IDs.. any solution for this?

Reply

DJ K-Ruck July 8, 2008 at 7:30 am

will this change the old users passwords as well or will this only change the new incoming users

Reply

Bharat Suneja July 8, 2008 at 7:39 am

@Shirish: What’s ‘INA’? Can you provide more details?

@Kuinten: This post shows how to create new mailboxes (that is, mailboxes with new user accounts in AD).

To simply create mailboxes for *existing user accounts* (no, it does not change passwords): Exchange Server 2007: Bulk mailbox-enabling users using Exchange Shell

Reply

DJ K-Ruck July 8, 2008 at 7:51 am

thanks, they were scared to use this new method if it would change all the passwords for new and old users, we will give this a try

Reply

DJ K-Ruck July 8, 2008 at 8:47 am

Hopefully this is the last question, They said that we would have to run 2 different shells win and the exchange shell is there a way to run both without having to do 2 different programs, i guess like a all in one program that will do both

Reply

Anonymous July 17, 2008 at 9:10 am

After I ran this it showed this ">>", does that mean that it did not run correctly?

Reply

Anonymous July 29, 2008 at 12:52 am

Thanks a lot Bharat!
Can you show me how to passing parameter to that .ps1 script? Let say I want the .csv file name to be passed from command line, not hard-coded in the script?

Reply

Anonymous August 12, 2008 at 12:18 pm

I have this script that I need to use to import about 3300 users.

## Section 1
## Define Database for new mailboxes
$db=”DC-MAILSRV01\Jeannette_City”

## Define User Principal name
$upndom=”wiu.k12.pa.us”

## Define OU for new users
$ou=”Jeannette City”

## Define CSV File with user information
$csvFile=”C:\Apps\IU Prodject\Jeannette SD.csv”

## Section 2
## Import csv file into variable $users
$users = import-csv $csvFile

## Section 3
## Function to convert Password string to secure string
function SecurePassword([string]$plainPassword)
{
$secPassword = new-object System.Security.SecureString

Foreach($char in $plainPassword.ToCharArray())
{
$secPassword.AppendChar($char)
}

$secPassword
}

## Section 4
## Create new mailboxes and users
foreach ($i in $users)
{
$sp = SecurePassword $i.password
$upn = $i.finit + “@” + $upndom
$display = $i.firstname + ” ” + $i.lastname
New-Mailbox -Password $sp -Database $db -UserPrincipalName $upn $i.finit -FirstName $i.finit -LastName $i.lastname -OrganizationalUnit $OU
}

The 1 problem that I have is, I would like to have first int last name for each account. and this is just making it with the first name, how can I extract that from the first name.

Reply

dre January 9, 2009 at 4:48 am

my users reside in the ou

europe\users
so the ou users exists more than once. how should i adjust my csv and my script??

Reply

Anonymous June 25, 2009 at 11:08 pm

Just a note for using Greek characters ( i suppose other international formats too) with exchange management shell and importing csv files or similars you must save the csv file as UTF-8

Stratis

Reply

Anonymous September 30, 2009 at 1:51 pm

I am looking to have a script to fill in the following information for me.

First Name, Last Name, Description, Office, Telephone Number, Title, Department, and company. We also will need to be able to add an 2ndary pop mail account if possible. Any help would be appreciated.

Reply

Anonymous October 22, 2009 at 2:48 am

Hi, I would like to know if that possible to pass the Password from the CSV just like a "password" column?
I try that with -password $_.password but it doesn't work.

Thanks

Reply

Anonymous November 15, 2009 at 10:33 pm

HI can somebody tell me if i can update title and other information through csv file

Reply

Bharat Suneja November 15, 2009 at 11:13 pm

@Anonymous from October 22: Password needs to be a secure string. You can convert plaintext to secure string. If your password field/column name is password:

Import-CSV MyUsersFile.csv | foreach {$password = ConvertTo-SecureString $_.password -AsPlainText -Force; new-mailbox -alias $_.alias -name $_.name -userPrincipalName $_.UPN -database "Mailbox Database" -org Users -Password $password}

Reply

Anonymous January 4, 2010 at 1:07 pm

Hi all,

We can create users+mailboxes with just one command line
Step 1: $password = read-host "enter password" -assecurestring
Step 2: enterpassword:test@123
OU = Lab
MBX= mbdb3
Domain: Fog.com

1….10 | foreach {new-mailbox -alias "lab$_" -name "lab$_" -password $password -database "mbdb3" -organizationalunit lab -userprincipalname "[email protected]"}

Reply

Anonymous March 31, 2010 at 4:21 am

Using Exchange 2010 Management Shell Executed the same script to import bulk users but getting following error:
Import-CSV bulk_import.csv| ForEach-Object {new-mailbox -alias $_.alias -name $_.name -userPrincipalName $_.UPN -database "Mailbox Database" -org Users -Password $Password -TemplateInstance $Template}
A positional parameter cannot be found that accepts argument '-TemplateInstance'.
+ CategoryInfo : InvalidArgument: (:) [New-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,New-Mailbox

Any solution to this error

Reply

kns May 4, 2010 at 1:22 am

Hello Bharat
I have already created the bulk exchange 2007 mailboxes. I am now trying to create the AD Home folders for these mailboxes/users. Would you have any idea how I can get this done easily? Thanks for your helpful post.

Reply

Todd September 15, 2010 at 4:32 pm

It is the best article I can find for this, a little extensive explanation is very helpful for the trick. Most of articles are written for authors’ self-reading and it is hard to understand for others.

Thanks.

Reply

Marty February 9, 2011 at 1:11 pm

For Anon from March 31 2010. Templates do not work in Exchange 2010.

Reply

Matt February 23, 2011 at 9:11 am

Thanks — this article and a couple of the previous comments were most helpful!

Reply

ruhi November 29, 2011 at 8:54 am

hi,

Many thanks for this script. is it possible to set the primary smtp address on the end of this script so it changes the address from @.com to @.co.uk ?

Reply

john January 25, 2013 at 5:07 am

getting the following message:
Import-Csv : Cannot open file “C:\CreateRecipients.csv”.
At line:1 char:11
I am in the root directory of C:\ and the .csv is in the root directory.

Reply

Eric April 3, 2013 at 10:13 am

What a great tutorial!

Reply

Leave a Comment

{ 4 trackbacks }

Previous post:

Next post: