Skip to content

Instantly share code, notes, and snippets.

@emarte91
Created November 1, 2018 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emarte91/6ddb92d6b4c8982037762cb5ab10b2e3 to your computer and use it in GitHub Desktop.
Save emarte91/6ddb92d6b4c8982037762cb5ab10b2e3 to your computer and use it in GitHub Desktop.
Create multiple Active Directory accounts with a csv
Import-Module ActiveDirectory
Import-Csv \\Pathname\ADbulk.csv | ForEach-Object {
$firstName = $_.firstname
$lastName = $_.lastname
$name = $firstName + ' ' + $lastName
$samAccountName = $firstName[0] + $lastName
$userPrincipal = $samAccountName + "@companyname.com"
$ouPath = "OU=Base,OU=Sites,DC=rome,DC=local"
$password = (ConvertTo-SecureString "Password1" -AsPlainText -Force)
$enable = $true
try {
New-ADUser -Name $name -GivenName $firstName -Surname $lastName -SamAccountName $samAccountName -UserPrincipalName $userPrincipal -Path $ouPath -AccountPassword $password -Enabled $enable -ChangePasswordAtLogon $true
Write-Host "Successfully created a user account for $name" -ForegroundColor Green
}
catch {
Write-Warning "Something went wrong with $name's account creation"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment