Create bulk users from .csv in AD using Powershell

While working as System Administrator, you come across with a situation where you have to create bulk users from .csv as new hires into your company’s Active Directory Users & Computers. Doing this task manually can be hectic and time consuming.

There are many Powershell Scripts out there which sometimes seems confusing and doesn’t even work. Let’s check the easy way out to create bulk users in Windows Server local Active Directory.

Easy Steps to Add Bulk Users into Active Directory:

a) Create your .csv similar to the below format. To download below sample as .csv file, click here

GivenName Surname samAccountName DisplayName Password
Ajey Gupta Ajey.gupta Ajey Gupta Pass@123
Ashish Pandey Ashish.Pandey Ashish Pandey Pass@123
Deepak Shaw Deepak.shaw Deepak Shaw Pass@123
Asha Sharma Asha.Sharma Asha Sharma Pass@123
Manisha Verma Manisha.Verma Manisha Verma Pass@123
Alexa Simmons Alexa.Simmons Alexa Simmons Pass@123
Alisa Ward Alisa.Ward Alisa Ward Pass@123
Lovely Singh Lovely.Singh Lovely Singh Pass@123
Pooja Jain Pooja.Jain Pooja Jain Pass@123
Vignesh Kumar Vignesh.Kumar Vignesh Kumar Pass@123

Friends, I have added only 10 users in the .csv file. You can add as many users in the above format.

b) After downloading the sample .csv, edit it by adding your real users Given names, Surname, SamAccountName, DisplayName and Passwords.

c) Run the script to create bulk users from .csv into your Active Directory Users & Computers.


Download Script: https://bit.ly/BulkUsersScript

d) Open Powershell with Run as Administrator and go to the location where the script CreateNewUsers.ps1 is downloaded.

e) Let’s assume you downloaded it on C Drive, then type C:\.\CreateNewUsers.ps1 and press enter, it will ask you to browse to the location where .csv file is located. Select your .csv file and Press Ok.

f) Finally refresh your Active Directory Users & Computers and you will find a new Organizational Unit created with the name “NewUsers” and all your new hire users from .csv will be added inside it.

You can edit the name of the OU, move them to different OU, or can even change the name of the OU in the script before running it.

You can also copy the script & edit it directly in the notepad before running it (script in .ps1 is also available above):

#Script to create Bulk Users into AD 

$ErrorActionPreference = “SilentlyContinue”

function Select-FileDialog
{
param([string]$Title,[string]$Directory,[string]$Filter=”CSV Files (*.csv)|*.csv”)
[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) | Out-Null
$objForm = New-Object System.Windows.Forms.OpenFileDialog
$objForm.InitialDirectory = $Directory
$objForm.Filter = $Filter
$objForm.Title = $Title
$objForm.ShowHelp = $true

$Show = $objForm.ShowDialog()

If ($Show -eq “OK”)
{
Return $objForm.FileName
}
Else
{
Exit
}
}

$FileName = Select-FileDialog -Title “Import an CSV file” -Directory “c:\”

$ExchangeUsersOU = “OU=NewUsers”

$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()
$DomainDN = (([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).Domains | ? {$_.Name -eq $domain}).GetDirectoryEntry().distinguishedName
$final = “LDAP://$DomainDN”
$DomainPath = [ADSI]”$final”
$cOU = $DomainPath.Create(“OrganizationalUnit”,$ExchangeUsersOU)
$cOU.SetInfo()

$UserInformation = Import-Csv $FileName

$OUPath = “LDAP://$ExchangeUsersOU,$DomainDN”
$UserPath = [ADSI]”$OUPath”
Write-Host “—————————————————————“
Write-Host “Creating New Users”
Write-Host “Version 1.1”
Write-Host “—————————————————————“

Foreach ($User in $UserInformation){

$CN = $User.DisplayName
$SN = $User.Surname
$Given = $User.givenName
$samAccountName = $User.samAccountName
$Display = $User.DisplayName

$LABUser = $UserPath.Create(“User”,”CN=$CN”)
Write-Host “Creating User: $User.samAccountName”
$LABUser.Put(“samAccountName”,$samAccountName)
$LABUser.Put(“sn”,$SN)
$LABUser.Put(“givenName”,$Given)
$LABUser.Put(“displayName”,$Display)
$LABUser.Put(“mail”,”$samAccountName@$domain”)
$LABUser.Put(“description”, “New User – created via Script”)
$LABUser.Put(“userPrincipalName”,”$samAccountName@$domain”)
$LABUser.SetInfo()

$Pwrd = $User.Password

$LABUser.psbase.invoke(“setPassword”,$Pwrd)
$LABUser.psbase.invokeSet(“AccountDisabled”,$False)
$LABUser.psbase.CommitChanges()

}
Write-Host “Script Completed”

I hope the above script will help you make your life easier. In case you have any specific requirements, do let me know by emailing me at [email protected].

Thanks a ton for reading my above blog. If you have any comments, suggestions, or feedback please spare a second to post it.

Must check:

a) Outlook connects to O365 Mailbox bypassing Autodiscover.

b)  Create Skillshare Premium Account.

c) Create Lynda Premium Account.

d) Create Pluralsight Premium Account. 

Subscribe Us

Total Page Visits: 7398 - Today Page Visits: 11

Add a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.