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 [email protected]
Ashish Pandey Ashish.Pandey Ashish Pandey [email protected]
Deepak Shaw Deepak.shaw Deepak Shaw [email protected]
Asha Sharma Asha.Sharma Asha Sharma [email protected]
Manisha Verma Manisha.Verma Manisha Verma [email protected]
Alexa Simmons Alexa.Simmons Alexa Simmons [email protected]
Alisa Ward Alisa.Ward Alisa Ward [email protected]
Lovely Singh Lovely.Singh Lovely Singh [email protected]
Pooja Jain Pooja.Jain Pooja Jain [email protected]
Vignesh Kumar Vignesh.Kumar Vignesh Kumar [email protected]

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”,”[email protected]$domain”)
$LABUser.Put(“description”, “New User – created via Script”)
$LABUser.Put(“userPrincipalName”,”[email protected]$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: 1099 - Today Page Visits: 5

Add a Comment

Your email address will not be published. Required fields are marked *

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

%d bloggers like this: