Friday, January 21, 2011

Create AD Users in bulk with PowerShell

This code snippet allows you to easily create multiple users. This is helpful when you need a bunch of test accounts on your DEV environment.

function CreateUser($userName, $password)
{
try
{
$adObject = "WinNT://" + [System.Environment]::MachineName + ",computer" #Assumes computer name
$AD = New-Object System.DirectoryServices.DirectoryEntry($adObject)
$NewUser = $AD.Children.Add($userName, "user")
$NewUser.Invoke("SetPassword", $password)
$NewUser.CommitChanges()
Write-Host "User " + $username" + created successfully."
}
finally
{

}
}

CreateUser "IntranetOwner" "pass@word1"
CreateUser "IntranetMember" "pass@word1"
CreateUser "IntranetVisitor" "pass@word1"

CreateUser "FIOwner" "pass@word1"
CreateUser "FIMember" "pass@word1"
CreateUser "FIVisitor" "pass@word1"

CreateUser "HROwner" "pass@word1"
CreateUser "HRMember" "pass@word1"
CreateUser "HRVisitor" "pass@word1"

CreateUser "ISOwner" "pass@word1"
CreateUser "ISMember" "pass@word1"
CreateUser "ISVisitor" "pass@word1"

CreateUser "MKTOwner" "pass@word1"
CreateUser "MKTMember" "pass@word1"
CreateUser "MKTVisitor" "pass@word1"

No comments:

Post a Comment