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

GivenNameSurnamesamAccountNameDisplayNamePassword
AjeyGuptaAjey.guptaAjey Gupta[email protected]
AshishPandeyAshish.PandeyAshish Pandey[email protected]
DeepakShawDeepak.shawDeepak Shaw[email protected]
AshaSharmaAsha.SharmaAsha Sharma[email protected]
ManishaVermaManisha.VermaManisha Verma[email protected]
AlexaSimmonsAlexa.SimmonsAlexa Simmons[email protected]
AlisaWardAlisa.WardAlisa Ward[email protected]
LovelySinghLovely.SinghLovely Singh[email protected]
PoojaJainPooja.JainPooja Jain[email protected]
VigneshKumarVignesh.KumarVignesh 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: - Today Page Visits:

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: