I recently deployed Exchange 2010 which required a large ammount of mail enabled users accounts to be created. I created the user accounts and mailboxes in one step by using the Import-CSV cmdlet combined with the New-Mailbox cmdlet which made this really easy and created around 90 user accounts and mailboxes in about 30 secounds, I cant really say that I would call this a script because it is only three lines but it saved a mamoth amount of time.
Throughout this blog I am going to be using the ` (backtick) to break up a single line of code onto multiple lines, this is used in Richard Siddway's Book Powershell in Practice to make single long lines of code easier to read and I am going to adopt this here to try and make whatever I post easier to read.
$password = Read-Host -Prompt "Please enter the password to give all users” -AsSecureString
$filelocation = Read-Host -Prompt "Enter the Full path and filename of the CSV file"
Import-Csv $filelocation | ForEach {New-Mailbox -Name $_.name -ExternalEmailAddress `
$_.externalEmailAddress -OrganizationalUnit $_.OU -Password $password -UserPrincipalName $_.UPN `
-Alias $_.alias -Firstname $_.firstname -LastName $_.lastName -DisplayName $_.displayName - `
samAccountName $_.samAccountName -ResetPasswordOnNextLogon $false}
- To Start with we need to read in a password for the user accounts. You have set a generic password on all the accounts when they are created otherwise they are created in a disabled state. To do this without hard coding the password into the script you need to use Read-Host cmdlet to read in a password from the user and convert it to a securestring, you can then use this variable as a parameter for New-Mailbox.
- The second line declares a variable with the full path and file name of the CSV file that contains the information that we are using Import-CSV cmdlet to import.
- The Third line imports the contents of the CSV file from the location that was specified and then uses a for each loop to execute the New-Mailbox cmdlet on the information passed in from each row of the CSV file.
This was the first time I had used PowerShell to write a script (using the term loosly) from scratch, rather than googling a one liner to fix an issue and there were a few things which tripped me up when I was troubleshooting this.
Firstly at the time I did not know that the "$_." represented the current object in the pipeline, meaning, in this example $_.name was the object read in by Import-CSV from the row called name in my CSV file. I struggled with this for a while as I originally thought it was a way to reference a variable within the loop, this is not correct. Make sure when you are using parameters in the New-Mailbox loop that there is a corresponding row in the CSV with this information in it, all the New-Mailbox cmdlet is doing is creating an object in AD, creating the mailbox for the user object and populating the properties associated with each IE: Diaplayname, Alias, Firstname, Lastname, OU etc.
Also one other issue I ran into when testing this script was when I copied the script from notepad and then pasted it straight into the command shell. This actually just drops whatever you paste into the prompt line by line, meaning the first line of the script when it asks for a password was actually getting the rest of the script pasted into the Read-Host field. This had me confused for a long time. Once I saved the script and went to run it (.\CreateAccounts.ps1), there was a pause for the actual password to be entered.
These both seem like very rookie mistakes but I do not have a scripting background and I just wanted to point these out to anyone else that may run into the same problem.