PowerShell to bulk create users

Discuss automation techniques, coding, development, tips, scripts, and more.
Post Reply
User avatar
Posts: 3
Joined: Fri Nov 08, 2019 1:47 pm

PowerShell to bulk create users

Post by ptak » Sat Nov 09, 2019 8:46 am

This is just something I tossed together a long time ago (it should still work tho). It can create bulk sets of Active Directory users from a CSV.

Code: Select all

Import-Module ActiveDirectory

# Path to your user csv
$Users = Import-Csv -Delimiter "," -Path "C:\Users\ptak\Desktop\users.csv"

# For each line in CSV, attempt to create a user
ForEach ($Item in $Users) {
$errorCode = "0"

Try {
 $Name = $Item.FirstName + " " + $Item.Lastname
 $DisplayName = $Item.FirstName + " " + $Item.Lastname
 $SamAccountName = $Item.SamAccountName
 $GivenName = $Item.FirstName
 $SurName = $Item.Lastname
 $Password = $Item.Password
 $Description = $Item.Description
 $OU = $Item.OU

 # Lets us know what is being attempted
 Write-Host "Attempt: $Name"

 # Create account
 New-ADUser -Name $Name -DisplayName $DisplayName -SamAccountName $SamAccountName -GivenName $GivenName -Surname $SurName -Description $Description -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path $OU -ChangePasswordAtLogon $false -PasswordNeverExpires $true
    # Catch erorr if user already exists
    Catch [Microsoft.ActiveDirectory.Management.ADIdentityAlreadyExistsException] {
    Write-Host "Skipped: $Name (already exist)" -ForegroundColor Red
    $errorCode = "1"

    # Catch other errors and throw exception message
    Catch {
    Throw $Error[0]
    $errorCode = "1"

    # If error code is still 0, we know the account was created
    if ($errorCode -ne "1") {
    Write-Host "Created: $Name" -ForegroundColor Green

The greatest wealth is to live content with little

User avatar
Posts: 27
Joined: Thu Nov 07, 2019 11:01 am
Location: United States

Re: PowerShell to bulk create users

Post by phdavis » Sat Nov 09, 2019 8:57 am

Thank you for the share :D I've used many scripts just like this to help speed up testing.

Post Reply