lunedì 1 luglio 2013

Simple Bulk User Creation in AD Using powershell

After some testing I found the easiest way to bulk create users in AD using: powershell new cmdlet and some DSQUERY & DSMOD commands.

First of all create an excel file with this header columns: 
Name,Display Name,Description,sAMAccountName,given Name,Surname,Title,physicalDeliveryOfficeName,
OfficePhone,givenName,displayName,Company,
department,userPrincipalName,Path,
EmailAddress,City,State,Enabled,Country

Fill the excel file with your users detail. 

Notes:
userPrincipalname: usually I put name.surname@domain.local
State: is the "state/province" field in AD Gui
Country: is the combo box Country/Region, this field must be in form "US" for usa or "IT"for Italy.
Path: is the Distinguisced Name of the Organizational Unit inside your forest so must be in form "OU=my users, DC=test,DC=local"



The colums are not all mandatory but this list fill almost all the useful fileds for users.


Then export the file as CSV, if your operating system is not english you should remember to change the field separator to a comma. In my italian version of excel the field separator is: ";" or use the -delimiter ";" switch on the command line.
When you export from excel correct the " sign in the csv should be in the form: 
"""

Done that connect to your w2008 DC and  execute the "Active Directory Module for Windows PowerShell" from Administrative tools or a normal power shell console but remember to import module ActiveDirectory

Then if your CSV file was done correctly run: 
Import-Module ActiveDirectory
import-CSV | New-ADUser 

You should see after a refresh the new created users in the path you selected. I suggest to use a temporary OU set adhoc for that (see below)

Unfortunately the field AccountPassword do not set corrctly the password so the easiest way to change is to run: 
DSQUERY user "OU=MyNewUsers,OU=MyCompanyAccount,DC=mydomain,DC=local" | DSMOD user -pwd

That's IT!