Microsoft

Blog Categories

Subscribe to RSS feed

Archives

Creating Bulk AD User Accounts using PowerShell

I am sure most administrators have received a request to create several AD accounts due to acquisitions, mergers, or for other reasons. I had one of those requests which consisted of populating an AD environment that was to be used for Directory Synchronization with BPOS/Office 365. There are a lot of Bulk AD scripts out in this little world wide web of ours and there are also third party applications that you could use too (for a price); however, in the pursuit of scripting, the following came to be.

The script will create all user accounts with the following attribute information:

  • sAMAccountName
  • mail
  • userPrincipalName
  • proxyAddresses (default SMTP)
  • givenName (First Name)
  • sn (Last Name)
  • Initials
  • Display Name
  • Name

One really cool thing about this script that is handy is as it loops through each user in the users.csv file, it will type out the display name of the user it is creating and will also provide the total number of users that were created upon completion. A nice little verification at the end.

The following will need to be modified to be specific to your environment.

  • $text = “C:ADusers.csv” – Name of Import file
  • $dc = “dc=DOMAIN,dc=COM” – Domain Controller (FQDN)
  • $ou = “ou=TestOU” – Organizational Unit location for users to be created
  • $user.psbase.invokeset(“AccountDisabled”, “True”) – You may want to set this variable to “False” if you want these accounts already enabled

What I did not include is an error output file. During the script process; however, errors will be highlighted in red. You can scroll up to see the error, which usually is object already exists.

** Always test any script in a lab environment first **

# ==============================================================================================
#
# Microsoft PowerShell Source File – Created by UltraEdit
#
# NAME: ADBulkAdd.ps1
#
# AUTHOR: Rene Strawser, PointBridge
# DATE : 06/20/2011
#
# COMMENT: Use this script to add bulk AD user accounts
#
# DIRECTIONS FOR USE:
# Create a users.csv with the following headers user,mail,givenName,sn,initials
# Populate the users.csv
# Create a folder titled AD and place the users.csv in that location
#
# REVISION HISTORY:
#
# Rev Who Description
# — — ——————————————
# 000 RS Initial release
# 001 RS Update – Total created
#
# ==============================================================================================
#
$text = “C:ADusers.csv”
$Class = “User”
$dc = “dc=DOMAIN,dc=COM”
$ou = “ou=TestOU”
$tu = 0
Import-Csv $text | foreach {
if($_.user) {
$name = $_.givenName + ” ” + $_.initials + ” ” + $_.sn
$ADSI = [ADSI]“LDAP://$ou,$dc
$cnuser=”cn=”+$name
$pa= “SMTP:” + $_.mail
Write-Host “creating user: ” $name
$User = $ADSI.create($Class,$cnuser)
$User.put(“SamaccountName”, $_.user)
$User.put(“mail”, $_.mail)
$User.put(“proxyAddresses”, $pa)
$User.put(“userPrincipalName”, $_.mail)
$User.put(“givenName”, $_.givenName)
$User.put(“sn”, $_.sn)
if($_.initials) {
$User.put(“initials”, $_.initials)
}
$User.put(“displayName”, $name)
$User.setInfo()
$user.psbase.invokeset(“AccountDisabled”, “True”)
$User.setInfo()
$tu += 1
}
}
Write-Host
Write-Host “Total number of users created = ” $tu

Tags: , , ,

Leave a Reply