Back to all recipes
Level: MidLast updated:
Bulk-create users from a CSV file
# Случайный пароль для каждого пользователя — ничего не хардкодим
Add-Type -AssemblyName System.Web
Import-Csv .\new-users.csv | ForEach-Object {
$pwd = [System.Web.Security.Membership]::GeneratePassword(16, 4)
New-ADUser -Name $_.Name -SamAccountName $_.SamAccountName `
-Path $_.OU -AccountPassword (ConvertTo-SecureString $pwd -AsPlainText -Force) `
-ChangePasswordAtLogon $true -Enabled $true
[pscustomobject]@{ User = $_.SamAccountName; TempPassword = $pwd }
} | Export-Csv .\new-users-passwords.csv -NoTypeInformation -Encoding UTF8Why it works this way
Onboarding a whole department at once. The file needs Name,SamAccountName,OU columns.
Caution
Validate the CSV first: one wrong column creates dozens of accounts in the wrong OU.
Watch out
- Run with -WhatIf first: see what would be created without creating it.
- The script generates a random password per user. The password file is sensitive: delete it right after handover.