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.
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
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.
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 }
How would you create bulk mailboxes for users already in the active directory?
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?? :-)
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)
Great Blog!
Is it also possible to put more than one alias address in the script?
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
Worked beautifully. Thanks so much for the intro to PowerShell that I needed!
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.
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
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?
will this change the old users passwords as well or will this only change the new incoming users
@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
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
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
After I ran this it showed this ">>", does that mean that it did not run correctly?
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?
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.
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??
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
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.
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
HI can somebody tell me if i can update title and other information through csv file
@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}
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]"}
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
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.
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.
For Anon from March 31 2010. Templates do not work in Exchange 2010.
Thanks — this article and a couple of the previous comments were most helpful!
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 ?
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.
What a great tutorial!
{ 4 trackbacks }