Skip to content

Instantly share code, notes, and snippets.

@emarte91
Last active September 12, 2018 09:05
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/65b1adbabc7bcf007eead9986c6f050f to your computer and use it in GitHub Desktop.
Save emarte91/65b1adbabc7bcf007eead9986c6f050f to your computer and use it in GitHub Desktop.
Basic AD account creator | Modify to your environment needs
$firstName = Read-Host "What is the users first name?"
$lastName = Read-Host "What is the users last name?"
# Default username setting is first letter of first name and full last name Ex: John Smith = Jsmith
$recommendedUser = $firstName[0] + $lastName
#Password set to "Password1" feel free to change
$password = (ConvertTo-SecureString -AsPlainText "Password1" -Force)
$fullName = "$firstName $lastName"
try{
#Checks if account already exists, if it does it will ask you to create a samaccountname
Get-AdUser $recommendedUser | Select SamAccountName
Write-Warning "Username already exist."
$newUsername = Read-Host "What would you like the new username to be?"
Write-Host "New username will be $newUsername"
}
catch{
$newUsername = $recommendedUser
Write-Host "Perfect! Setting $recommendedUser as the username" -ForegroundColor Green
}
finally{
#Feel free to add or modify as needed
New-ADUser -Name $fullName -GivenName $firstName -Surname $lastName -DisplayName $fullName -SamAccountName $newUsername -Path "Set OU Path Here" -AccountPassword $password -ChangePasswordAtLogon $True -Enabled $True
Write-Host "Successfully created the user account" -ForegroundColor Green
pause
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment