.comment-link {margin-left:.6em;}
A Collection of Random Thoughts
Tuesday, August 22, 2006
Creating a new mailbox using Powershell and specifying the password

So you want to create a new mailbox with Exchange 2007.  No biggie - you can create it either using the Management Console (GUI), or via the Management Shell (Powershell).  That's boring, though.  What if you want to create it via the Shell AND specify the password?  Ok - let's do it.

First, let's take a look at the Help for the new-mailbox command usage.

New-Mailbox -Name <String> -Database <DatabaseIdParameter> -OrganizationalUnit <OrganizationalUnitIdParameter> -Password <SecureString> -UserPrincipalName <String> [-Alias <String>] [-DisplayName <String>] [-DomainController <String>] [-FirstName <String>] [-Initials <String>] [-LastName <String>] [-ManagedFolderMailboxPolicy <MailboxPolicyIdParameter>] [-MobileMailboxPolicy <MailboxPolicyIdParameter>] [-ResetPasswordOnNextLogon <$true | $false>] [-SamAccountName <String>] [-TemplateInstance <MshObject>]

Notice that the -Password option needs to be entered as a Secure String.  That means that you cannot enter it as plain text during the command.  Now, let's take a look at one of the examples provided in the help.


$password = Read-Host "Enter password" -AsSecureString

New-mailbox -UserPrincipalName chris@contoso.com -alias chris -database "Storage Group 1\Mailbox Database 1" -Name ChrisAshton -OrganizationalUnit Users -password $password -FirstName Chris -LastName Ashton -DisplayName "Chris Ashton"

This example shows how to create a new mailbox using the -password option.  Note that the password is not actually set to $password.  Rather, $password is a variable that has been set by the first command.  As I mentioned, when you enter the password, it cannot be in plain text. If you try to, you'll receive the following error.

New-Mailbox : Cannot bind parameter 'Password'. Cannot convert value "password" to type "System.Security.SecureString". Error: "Invalid cast from 'System.String' to 'System.Security.SecureString'."
At line:1 char:70

As indicated in the example, you need to define the $password variable.  This is done by using the first string from the example.  When you run the command

$password = Read-Host "Enter password" -AsSecureString

you will be presented with a prompt stating

Enter password: **********  (note that the password I entered is entered as a secure string - the text is hidden with asterisks)

Once you've entered this password, it's stored as a secure string.  You now just need to specify the password using the $password variable you've defined.  The downside to this method is that you would have to use the same password for each user that you created using this variable.  Note that the following fields are required when creating a new user account.  Also note that if you are creating a linked mailbox, a shared mailbox, or a resource mailbox, a password is not required as the account will be disabled.


Everything else is optional.

Comments: Post a Comment

Links to this post:

Create a Link

<< Home

Powered by Blogger