Bulk mailbox creation: Import passwords from a file

by Bharat Suneja

Automating bulk mailbox creation required fairly advanced scripting skills in Exchange 2003/2000. Thanks to the Exchange Management Shell (aka “the shell”) in Exchange 2010 and 2007, this task is greatly simplified. It doesn’t require any advanced scripting skills and it can be accomplished by relative newcomers to Exchange Server with very little knowledge of the shell.

Exchange Server 2007: Bulk creation of mailboxes using Exchange Management Shell shows you how to create bulk mailboxes using user data imported from a CSV file. A related post— Bulk mailbox creation revisited: Adding Active Directory attributes shows you how additional Active Directory attributes not included in the New-Mailbox/Set-Mailbox cmdlets can be populated.

When creating mailboxes using the New-Mailbox cmdlet, Exchange Shell requires the password to be of type System.Security.SecureString, derived from the SecureString class in the dot net framework. In the example in Exchange Server 2007: Bulk creation of mailboxes using Exchange Management Shell, we use the same password for all accounts. We also prompt the admin to enter that password using the Read-Host cmdlet, as shown below:

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

When the admin running the command or script enters the password, powershell masks the password by displaying a * for each character entered.

One frequently asked question when discussing bulk mailbox creation is: how do I import passwords from a text file? Of course, saving passwords in a text file isn’t very secure, but there may be cases where you need to do this temporarily— particularly when you want to create mailboxes/user accounts in bulk and don’t want to assign the same password to all accounts. When doing so, it’s recommend to set the account to change password on next logon. There may also be other scenarios where you need to import passwords from a text file, so I’ll leave the security aspect of this up to you.

The first step to importing passwords from the text file is to add it as an additional column or field in the file. For example:

Alias,Name,UPN,Password
User_One,User One,[email protected],[email protected]
User_Two,User Two,[email protected],[email protected]
User_Three,User Three,[email protected],[email protected]

If you try to use the same command as shown in the previous post, and simply add the parameter -password and the value $_.password in the code block, it’ll fail.

Import-CSV CreateRecipients.csv | foreach {new-mailbox -alias $_.alias -name $_.name -userPrincipalName $_.UPN -database “Mailbox Database” -org Users -Password $_.password}
Cannot process argument transformation on parameter ‘Password’. Cannot convert the “[email protected]” value of type “System.String” to type “System.Security.SecureString”.
+ CategoryInfo : InvalidData: (:) [New-Mailbox], ParameterBindin…mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,New-Mailbox

Converting a string to a SecureString
To use the password field imported from the CSV file, you must first convert it to a SecureString. You can convert a string to a SecureString using the ConvertTo-SecureString cmdlet. When using the ConvertTo-SecureString cmdlet, you must specify that the source string is provided as cleartext by using the AsPlainText switch (not to be confused with the plaintext message format). The cmdlet also requires that you specify the Force switch to confirm you really want to do this— yes, you’ve just provided your consent to convert a plaintext string to a SecureString!

The modified command looks something like this:

Import-CSV CreateRecipients.csv | foreach {New-Mailbox -Alias $_.alias -Name $_.name -UserPrincipalName $_.UPN -Database “Mailbox Database” -Org Users -Password (ConvertTo-SecureString $_.password -AsPlainText -Force)}

To enforce a password change on next logon, add the ResetPasswordOnNextLogon parameter to the command:

Import-CSV CreateRecipients.csv | foreach {New-Mailbox -Alias $_.alias -Name $_.name -UserPrincipalName $_.UPN -Database “Mailbox Database” -Org Users -Password (ConvertTo-SecureString $_.password -AsPlainText -Force) -ResetPasswordOnNextLogon $true}

{ 4 comments… read them below or add one }

Anonymous November 25, 2009 at 3:46 am

I really want to thank u for this awsome article!!
u helped me big time on this one
thanks again

Reply

Anonymous March 23, 2010 at 5:03 am

Super nice , thank you,.

Reply

Mike - CSV File Format Guru August 7, 2010 at 10:36 am

Sounds great but first you need to create a CSV file out of your spreadsheet with passwords.

Reply

hosted exchange server August 13, 2010 at 8:41 am

Thanks for this tutorial. We had to turn away clients before due to the complex programming it took to do this, thanks for telling us it’s possible to simplify it now.

Reply

Cancel reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: