Created
November 1, 2018 01:37
-
-
Save emarte91/6ddb92d6b4c8982037762cb5ab10b2e3 to your computer and use it in GitHub Desktop.
Create multiple Active Directory accounts with a csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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