Quick and dirty PowerShell AD account creation script


Jonathan Frappier Virtxpert

I needed to setup various service accounts (http://technet.microsoft.com/en-us/library/ff641729%28v=ws.10%29.aspx) user account objects to run various applications and services for testing in my lab, after the 3rd right click >> New >> User I said to one of the many voices in my head – HEY SCRIPT THAT!

So, here it is, simple, basic, easy to edit.  I probably should change this to take input from a CSV file… and I probably will when I start typing service account names wrong.  Currently the OU path is hard coded, I hate typing that and I always put them in the same spot (again quick and dirty so don’t judge – it gets the job done!)

  • Prompts for a password to be used for all accounts created
  • Prompts for a list a service account names to be created
  • Loops though and creates all accounts entered, sets the password and enables the account.

Yes it is indented properly IRL, blame WordPress for the lack of indents here :)

powershell-new-aduser

 

powershell-new-aduser-proof

#Generic script to bulk create AD accounts. Prompts for service accounts and password to be used on all accounts.
#If need be, can be modified to created groups and add user accounts to groups.

#OU Path Variable, change to your desired location
$oupath = "OU=serviceaccounts,DC=lab2,DC=local"
#Password
$pw=(Read-Host "Enter the password (will be used for all accounts)")

#Get service account names
$svcaccts = @()
do
{
$input = (Read-Host "Enter service account name & press enter (no value and enter to end)")
if ($input -ne '') {$svcaccts += $input}
}
until ($input -eq '')

ForEach ($svcacct in $svcaccts)
{
#Creates new account
New-ADUser -Name $svcacct -Path $oupath -CannotChangePassword $true -PasswordNeverExpires $true

#Sets account password
Set-ADAccountPassword -Identity $svcacct -NewPassword (ConvertTo-SecureString -AsPlainText $pw -Force)

#Enable account
Enable-ADAccount -Identity $svcacct
}

Quick and dirty PowerShell AD account creation script