0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Create Bulk Users from CSV File

Use Import-Csv 'users.csv' | ForEach-Object { New-ADUser -Name $_.Name -GivenName $_.GivenName -Surname $_.Surname -SamAccountName $_.SamAccountName -UserPrincipalName $_.UserPrincipalName -AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -Force) -Enabled $true } to create bulk users from a CSV file in PowerShell.
📋

Examples

InputCSV file with one user: Name=John Doe, GivenName=John, Surname=Doe, SamAccountName=jdoe, UserPrincipalName=jdoe@example.com, Password=Pass123!
OutputUser 'John Doe' created successfully in Active Directory.
InputCSV file with two users: Jane Smith and Bob Lee with their details
OutputUsers 'Jane Smith' and 'Bob Lee' created successfully in Active Directory.
InputCSV file with missing Password field
OutputError: Password field missing or empty for one or more users.
🧠

How to Think About It

First, read the CSV file containing user details. Then, for each user record, create a new user account by passing the relevant fields to the user creation command. Handle passwords securely by converting them to a secure string. Finally, enable the user accounts.
📐

Algorithm

1
Import the CSV file containing user data.
2
For each user record in the CSV, extract user details like name, username, and password.
3
Convert the plain text password to a secure string.
4
Create a new user account with the extracted details and secure password.
5
Enable the user account.
6
Repeat for all users in the CSV.
💻

Code

powershell
Import-Csv 'users.csv' | ForEach-Object {
    $securePass = ConvertTo-SecureString $_.Password -AsPlainText -Force
    New-ADUser -Name $_.Name -GivenName $_.GivenName -Surname $_.Surname -SamAccountName $_.SamAccountName -UserPrincipalName $_.UserPrincipalName -AccountPassword $securePass -Enabled $true
    Write-Output "Created user: $($_.Name)"
}
Output
Created user: John Doe Created user: Jane Smith Created user: Bob Lee
🔍

Dry Run

Let's trace creating user John Doe from CSV through the script

1

Read CSV line

Name=John Doe, GivenName=John, Surname=Doe, SamAccountName=jdoe, UserPrincipalName=jdoe@example.com, Password=Pass123!

2

Convert password

Convert 'Pass123!' to secure string

3

Create user

Call New-ADUser with user details and secure password

NameSamAccountNamePassword (plain)Password (secure)
John DoejdoePass123!
💡

Why This Works

Step 1: Import CSV

The Import-Csv cmdlet reads the CSV file and converts each row into an object with properties matching the CSV headers.

Step 2: Convert Password

Passwords must be converted to a secure string using ConvertTo-SecureString to meet security requirements for user creation.

Step 3: Create User

The New-ADUser cmdlet creates a new Active Directory user with the provided details and enables the account.

🔄

Alternative Approaches

Using a CSV and a script with parameter validation
powershell
param([string]$CsvPath = 'users.csv')
Import-Csv $CsvPath | ForEach-Object {
    if (-not $_.Password) { Write-Warning "Password missing for $($_.Name)"; return }
    $securePass = ConvertTo-SecureString $_.Password -AsPlainText -Force
    New-ADUser -Name $_.Name -SamAccountName $_.SamAccountName -AccountPassword $securePass -Enabled $true
    Write-Output "Created user: $($_.Name)"
}
Adds validation to check for missing passwords before creating users.
Using PowerShell DSC for bulk user creation
powershell
# DSC configuration example omitted for brevity; uses configuration to define users from CSV
More complex but allows declarative user management and idempotency.

Complexity: O(n) time, O(n) space

Time Complexity

The script processes each user once, so time grows linearly with the number of users in the CSV.

Space Complexity

Memory usage grows linearly with the number of users because all CSV data is loaded into memory.

Which Approach is Fastest?

Direct scripting with Import-Csv and ForEach-Object is fastest for simple bulk creation; DSC adds overhead but improves management.

ApproachTimeSpaceBest For
Import-Csv + ForEach-ObjectO(n)O(n)Simple, quick bulk user creation
Script with validationO(n)O(n)Safer creation with error checks
PowerShell DSCO(n)O(n)Declarative, repeatable user management
💡
Always test your script with a small CSV sample before running bulk user creation in production.
⚠️
Forgetting to convert passwords to secure strings causes user creation to fail.