
Administrators of the MS Exchange Server 2007/2010 might find themselves in need of creating multiple mail-enabled user accounts at once (e.g. for a group of new employees or for testing purposes). Manual creation of even a relatively small number of Active Directory users – followed by assigning a mailbox to every one of them – can take a lot of time, not to mention creating dozens or hundreds of such items. Fortunately there is a way to make this procedure automatic – at least to some extent.
1. Creating a file with the user list.
The first thing that needs to be prepared is a complete list of all new users and mailboxes in a CSV file (Comma Separated Values). The easiest way to do it would be to use Microsoft Excel, but even ordinary Notepad is able to create such a file. In the example below the users have been defined according to the following criteria:
Alias,Name,UPN
Additional values can be added here as well – virtually for every field available in the Active Directory. A detailed how-to explaining that can be found at the end of this article. The example CSV user list looks like that:
Alias,Name,UPN
User_1,User 1,user1@domain
User_2,User 2,user2@domain
User_3,User 3,user3@domain
etc.
The header of the list is very important – it cannot be omitted. Once the list is ready, it needs to be saved as a CSV file with a relatively simple name – it will be used later in the script pulling data from it. In this example it was called CreateMailboxes.csv.
2. Assigning the password.
The next step is to create a password for all the new users. Manually assigning a new password for every single user will be a great waste of time – it is better to do it automatically using the Exchange Management Shell instead. It can be achieved with the following script:
$Password=Read-Host “Enter Password” –AsSecureString
A notification asking for the password will be displayed. We can now input the desired phrase – it will become the password for all the new users from the CSV list. It can always be changed later according to individual needs.
3. Importing the CSV file and creating the mailboxes.
The final step involves importing the CSV file we have prepared earlier and creating the mailbox-enabled user accounts. To do that, we need to issue the following command in the Exchange Management Shell:
Import-CSV CreateMailboxes.csv | ForEach {New-Mailbox -Alias $_.alias -Name $_.name -userPrincipalName $_.UPN -Database “Mailbox Database” -OrganizationalUnit Users -Password $Password}
The script presented above can be modified according to specific needs. The Exchange Management Shell interprets the selected parts of this command in the following way:
- Import-CSV CreateMailboxes.csv – pulls data from the CSV file we prepared earlier. If the file was saved in a location different from the one Exchange Management Shell is running from, a full path to the CSV file needs to be included in the script as well, e.g. “c:\My Documents\CreateMailboxes.csv” (quotation marks are required).
- ForEach – retrieves data from every single line of the CSV file using the first one as the header. All the field names are treated as names of the Active Directory fields. Additional values can be easily added to the command – e.g. when we add a line Company $_.company it will force the script to search the CSV file for a column called Company and pull from it all the data for each row of our user list.
- New-Mailbox – this item is directly responsible for the creation of new mailboxes. It supports a whole range of parametres that can be definded for each user – a full list of all available values can be found on the Microsoft Technet website: New-Mailbox cmdlet syntax. The necessary values can be added to the CSV file and the script should include an additional command line matching them.
- OrganizationalUnit – this value defines the Active Directory Organizational Unit all the new users will be added to. In the example script all the users have been included in the main “Users” group. To split users between multiple AD groups, it will be necessary to create an additional column in the CSV list (called for example “OU”), and then complete the script with a command that will pull data from this very column for every single user: -org $_.OU.
- Password – assigns to each user the password retrieved from the $Password value created before.
I hope that this article will help to save a lot of time and effort that would be required while trying to manually perform all the procedures described above.
© All rights reserved. No part or whole of this article may not be reproduced or published without prior permission.