How to create bulk users in the active directory using PowerShell?

Using Random Account Name

To create bulk users in the AD using PowerShell, there are multiple methods. For example, let say if you want to create 50 sample users for the lab environment without considering the other required properties, then you can use the below command,

$pass = Read-Host "Enter Account Password " -AsSecureString 1..50 | foreach{ New-ADUser -Name "TempUser$_" -AccountPassword $pass -Path "OU=LabUsers,DC=labdomain,DC=local" -ChangePasswordAtLogon $true -Enabled $true -PasThru}

The above command will create 50 Temp users in the OU named LABUSERS and one time we need to enter the password while running the script and when users log in with the same password, they need to change password at logon individually.

Using Text File

Another way to create bulk users in the active directory is using the text files containing AD users. For example,

$pass = Read-Host "Enter Account Password " foreach($ADAccount in (Get-Content c:\temp\ADAccounts.txt)){ New-ADUser -Name $ADAccount -AccountPassword $pass - Path "OU=LabUsers,DC=labdomain,DC=local" -ChangePasswordAtLogon $true -Enabled $true }

In the above example, a list of AD Accounts in the text file called ADAccounts.txt is placed at c:\temp.

Using the CSV file

This is the best way to create multiple users account with multiple properties as a CSV file. We can store the data into the CSV file and can jump on each line to create the user account with that specific properties.

Suppose we have a CSV file stored in the folder C:\temp and the file name called NewUsers.CSV. The content of the file is as below.

Code is as below.

$pass = Read-Host "Enter AD user password " foreach($account in (Import-Csv C:\Temp\NewUsers.csv)){
New-ADUser -Name $account.Name ` -DisplayName $account.FirstName `
-Surname $account.Surname `
-EmployeeID $account.EMPNumber ` -Country $account.Country `
-Path "OU=LabUsers,DC=labdomain,DC=local" ` -Enabled $true `
-AccountPassword $pass ` -ChangePasswordAtLogon $true `
-PassThru }

Updated on: 20-Nov-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started