Skip to content

Instantly share code, notes, and snippets.

@Grimthorr
Created October 8, 2015 08:17
Show Gist options
  • Save Grimthorr/2d85f4fda1b7f7408ffe to your computer and use it in GitHub Desktop.
Save Grimthorr/2d85f4fda1b7f7408ffe to your computer and use it in GitHub Desktop.
PowerShell script to batch create (or import) users in Active Directory.
Import-Module ActiveDirectory
$Users = Import-Csv -Delimiter "," -Path ".\users.csv"
foreach ($User in $Users) {
$SAM = $User.Username
$Displayname = $User.Displayname
$Firstname = $User.Firstname
$Lastname = $User.Lastname
$OU = $User.Container
$UPN = $User.Username + "@contoso.com"
$Password = (ConvertTo-SecureString $User.Password -AsPlainText -Force)
New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName "$SAM" -UserPrincipalName "$UPN" -GivenName "$Firstname" -Surname "$Lastname" -AccountPassword $Password -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false -PasswordNeverExpires $true
Write-Host "Created user: $SAM"
}
Username Displayname Firstname Lastname Password Container
testuser Test User Test User Password1 OU=Test,OU=Users,DC=contoso,DC=com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment