How to Create Multiple Users at Once in Active Directory Using Powershell Part 1 of 2

Create Multiple Users at Once in Active Directory Using Powershell.

Powershell is already built into the latest versions of Windows, and it’s free, so why not use it?

How to create multiple users quickly, at once, in active directory using Powershell. Powershell is already built into the latest versions of Windows, and it’s free, so why not use it?

NOTE: I came up with an easier and more efficient method HERE.

Create Multiple Users at Once in Active Directory Using Powershell

Server: Windows Server 2008 R2

Powershell Code:

$objOU=[ADSI]“LDAP://DC=something,DC=com”;
$dataSource=import-csv “list.csv”;
foreach($dataRecord in $datasource) {
$cn=$dataRecord.FirstName + ” ” + $dataRecord.LastName
$sAMAccountName=$dataRecord.FirstName + “.” + $dataRecord.LastName
$givenName=$dataRecord.FirstName
$sn=$dataRecord.LastName
$sAMAccountName=$sAMAccountName.ToLower()
$displayName=$sn + “, ” + $givenName
$userPrincipalName=$sAMAccountName + “@something.com”;
#Additional Attributes
$objUser=$objOU.Create(“user”,”CN=”+$cn)
$objUser.Put(“sAMAccountName”,$sAMAccountName)
$objUser.Put(“userPrincipalName”,$userPrincipalName)
$objUser.Put(“displayName”,$displayName)
$objUser.Put(“givenName”,$givenName)
$objUser.Put(“sn”,$sn)
#Place the additional attributes into the record
$objUser.SetInfo()
$objUser.SetPassword(“password”) ;
$objUser.psbase.InvokeSet(“AccountDisabled”,$false)
$objUser.SetInfo()
}

—–

Code for the additional fields:

#Additional Attributes
$physicalDeliveryOfficeName=$dataRecord.Office
#Place the additional attributes into the record
$objUser.Put(“physicalDeliveryOfficeName”,$physicalDeliveryOfficeName)

Please feel free to leave a comment below. I would love to hear from you!

Related tutorials:

About Chris Davis

Computer / Technology enthusiast. Very passionate about Systems Administration. I enjoy helping others try and reach their goals. You can follow Chris on if you'd like.

Comments

  1. Leave a comment and let me know if it worked for ya! I will soon be posting a NEW and IMPROVED version of this Powershell tip to create lots of users from a csv file.

  2. Hey , I am completely new to AD and at work i am suppose to create 200 users . So could you please help on this ? (We have a excel sheet where first name, last name, user_id,login id email id and pass word of each user are given. I understood how to create csv file but from thereafter can u juz elaborate script ). Thanx :)

  3. thnx buddy :-)

  4. Kristian says:

    Hi! Nice script!
    Is it possible in your script to add (ChangePasswordAtLogon)?

  5. Hi
    Can you Plz help me why I get this error when I run this script?!

    Unexpected token ‘\’ in expression or statement.
    At C:\Untitled2.ps1:23 char:20
    + $objUser.SetInfo()\ <<<<
    + CategoryInfo : ParserError: (\:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

    thanks

  6. Dinan Natividad says:

    Great Guide!!!! BTW how do I add the email address for each users?

Leave a Reply