write a powershell script to create bulk users in windows 2022 AD
- User
write a powershell script to create bulk users in windows 2022 AD
Assistant
write a powershell script to create bulk users in windows 2022 AD
Here's a PowerShell script to create bulk users in Windows Server 2022 Active Directory. The script uses a CSV file for user details and requires the Active Directory module.
# Import Active Directory module
Import-Module ActiveDirectory
# Prompt user for inputs
$csvPath = Read-Host "Enter the path to the CSV file (e.g., C:\Users.csv)"
$ouPath = Read-Host "Enter the Organizational Unit path (e.g., OU=Users,DC=example,DC=com)"
# Get domain DNS root for UserPrincipalName
$domain = (Get-ADDomain).DNSRoot
# Prompt for default password (securely stored)
$securePassword = Read-Host "Enter the default password for all users" -AsSecureString
# Import CSV data
try {
$users = Import-Csv -Path $csvPath
}
catch {
Write-Error "Failed to import CSV file. Ensure the path is correct and the file exists."
exit
}
foreach ($user in $users) {
try {
# Create user account
New-ADUser `
-GivenName $user.GivenName `
-Surname $user.Surname `
-SamAccountName $user.SamAccountName `
-UserPrincipalName "$($user.SamAccountName)@$domain" `
-Name "$($user.GivenName) $($user.Surname)" `
-Path $ouPath `
-AccountPassword $securePassword `
-Enabled $true `
-ChangePasswordAtLogon $true `
-ErrorAction Stop
Write-Host "User $($user.SamAccountName) created successfully." -ForegroundColor Green
}
catch {
Write-Warning "Failed to create user $($user.SamAccountName): $_"
}
}
CSV File Format: Create a CSV file with the following columns (replace with your data):
GivenName,Surname,SamAccountName
John,Doe,jdoe
Jane,Smith,jsmith
Run the Script:
ChangePasswordAtLogon $true: Users must change their password on first login.Enabled $true: Accounts are enabled immediately.Department, Title, or Office by modifying the New-ADUser parameters.Password column in the CSV and replace $securePassword with:
$securePassword = ConvertTo-SecureString $user.Password -AsPlainText -Force
OU=Users,DC=example,DC=com.User jdoe created successfully.
User jsmith created successfully.
This script provides a robust starting point for bulk user creation in Windows Server 2022 AD. Adjust parameters and CSV columns as needed for your environment.