Create User account in Active Directory from Spread Sheet |Import CSV Excel

We have many ways to create users in active directory for example you can create users from command prompt, PowerShell, .LDF, .PS1 files etc. but all these methods are time consuming if you have to create large number of user in very short time. Using CSV file and .PS1 script file we can create a multiple users quickly.

 

How to create CSV file for creating user accounts in Active Directory:

Create a Excel file and made the following columns in it and fill out your users info, I just added the dummy info into it. After adding user info into excel save this file as CSV.

cnsAMAccountNameFirstNameLastName
AlexAlex.zAlexz
SamSam.jSamj
LaraLara.bLarab
ZaraZara.EveZaraEve

Download users.CSV file for creating user accounts using powershell.

 

How to create Script file for creating user accounts in PowerShell:

We need a .PS1 script file which point to above CSV file and add users from CSV file to active Directory.

Script code for importing users from CSV file to active directory is as follow:

Script.PS1

$ou=[ADSI] “LDAP://ou=HR,ou=Department,dc=w7cloud,dc=com”

$dataSource=import-csv “usersdb.csv”

foreach($dataRecord in $dataSource)

{

$cn=$dataRecord.cn

$sAMAccountName=$dataRecord.sAMAccountName

$givenName=$dataRecord.FirstName

$sn=$dataRecord.LastName

$displayName=$sn +”,”+$givenName

$userPrincipalName=$givenName+”.”+$sn+”@w7cloud.com”

 

$NewUser=$OU.create(”user”,”CN=”+$cn)

$NewUser.Put(”sAMAccountName”,$sAMAccountName)

$NewUser.Put(”userPrincipalName”,$userPrincipalName)

$NewUser.Put(”displayName”,$displayName)

$NewUser.Put(”givenName”,$givenName)

$NewUser.Put(”sn”,$sn)

$NewUser.SetInfo()

$NewUser.SetPassword(“W@q@$98765”)

$NewUser.psbase.InvokeEet(“AccountDisabled”,$false)

$NewUser.Put(“company”, “W7CLOUD”)

$NewUser.SetInfo()

}

Download usersScript.PS1  file for creating user accounts using powershell

 

Now place both these file on same directory and open the PowerShell as administrator and run the following commands. I have save these two files usersdb.CSV and script.PS1 on my desktop.

PS C:UsersAdministratorDesktop> Set-ExecutionPolicy unrestricted.

PS C:UsersAdministratorDesktop> .script.ps1

After successful running of above 4 user will be appear in HR OU.

Create user accounts with Script CSV

Set-ExecutionPolicy unrestricted command enable the execution of script on your system and 2nd command will run the script file which will imports users from CSV files to active directory.

 

This first line in script.PS1 is representing the location where user accounts will be create, in our case it is HR OU which is an Sub-OU in Department OU in w7cloud.com DC. For more detail you can view the below image and visit create user with PowerShell.

script.ps1+CSV user account

 

PowerShell Command Syntax | Create Users Account in AD Using PowerShell 

Leave a Comment