Creating Active Directory Users with Windows PowerShell

Post to Twitter Post to Facebook Post to StumbleUpon

While it may seem easy to create Active Directory users using the management console, I still prefer doing it using scripts so as to make sure that they are done in a uniform, standard fashion (not to mention as fast as one can possibly do especially if you will be doing it for many users). I’ve referenced the scripts provided at the CodePlex site for ADSI and Active Directory for Windows PowerShell (full credit goes to them) to create users in Active Directory for Windows Server 2008. This also works for Windows Server 2003. While I may be a big fan of automation, it is important to highlight that processes are what makes automation really work. The reason I am saying this is that the CSV file can come from different sources, say, an intranet site where you ask employees to log in and key in their details. Having a process in place to make sure that users who would be entering their details in a standard way would eliminate the need to cleanse the data (I’m still thinking as a DBA here) in the long run. Plus, having a standard in place as an organization is starting out will make it flexible enough to scale as growth happens.

# define constants
= “,dc=domainName,dc=local”
$domainnb = “domainName” # domain netbios name
= “domainName.local”

$ADs_UF_NORMAL_ACCOUNT = 512 # Disables account and sets password required.

# Remember to enable the account before logging in

# Prompt user to enter the default passsword for the users
= ReadHost “Please enter default Password:” asSecureString

# Read the list of users from the CSV file
# Include other user properties in the CSV file as necessary

Importcsv users.txt | foreach
# Create user name based on FirstName and LastName column
in the CSV file
= $_.firstName + ” ” + $_.lastName

#Form the LDAP string based on the OU column from the CSV file
= “LDAP://OU=” + $_.OU + “,OU=domainName Domain Users” + $domainstr

$target = [ADSI] $strLDAP
= $target.create(“User”, “cn=” + $strUser)

#Define a naming convention for the login based on your corporate policy
#This one uses the first letter of the firstname and the lastname
= $_.firstName[0]+$_.lastName

#Define the other user attributes based on the columns defined in the CSV file
= $userID.ToString()
$newUser.givenName = $_.firstName
= $_.lastName
= $_.firstName + ” ” + $_.lastName
= $_.firstName[0]+$_.lastName + “@” + $domain
= $_.Email
= $_.Location
= $_.Designation
= $_.Designation


#Normal user that requires password & is disabled

WriteHost “Created Account for: ” $newUser.Displayname