Create a gist now

Instantly share code, notes, and snippets.

A PowerShell script to create a predictable list of sample active directory users and a sample active directory layout
function Create-UserList {
<#
.SYNOPSIS
This function provisions a predictable list of AD users and a typical AD structure for testing. Based on a script
from www.sharepointryan.com: https://sharepointryan.com/2012/03/14/creating-random-demo-users-in-active-directory-using-powershell/
.PARAMETER NumberOfUsers
The number of users to provision.
.EXAMPLE
PS C:\>Create-UserList -NumberOfUsers 50
#>
Param(
[int]$NumberOfUsers
)
#import the AD module
Import-Module ActiveDirectory -ErrorAction SilentlyContinue
$TopOU = "OU=" + (Get-AdDomain).Name + "corp," + (Get-AdDomain).DistinguishedName
$BaseOUs = "Kerberos Principals","Laptop Computers","Service Accounts","User Accounts","Servers"
$FNames = "Michael","Christopher","Matthew","Joshua","Daniel","David","Andrew","James","Justin","Joseph","Ryan","John","Robert","Nicholas","Anthony","William","Jonathan","Kyle","Brandon","Jacob","Tyler","Zachary","Kevin","Eric","Steven","Thomas","Brian","Alexander","Jordan","Timothy","Cody","Adam","Benjamin","Aaron","Richard","Patrick","Sean","Charles","Stephen","Jeremy","Jose","Travis","Jeffrey","Nathan","Samuel","Mark","Jason","Jesse","Paul","Dustin"
$LNames = "Harding","Smith","Jones","Peters","Fields","Waters","Cooke","Dobson","McGregor","Eaton","Lee","Gonzalez","Harris","Clark","Lewis","Robinson","Walker","Perez","Hall","Moore","Waldron","Saito","Wong","Stapleton","Stevens","Snowdon","Ali","Cousins","Johnson","Winters","Hassan","Williams","Brown","Hoyle","Miller","Davis","Garcia","Rodriguez","Wilson","Martinez","Anderson","Taylor","Thomas","Hernandez","Chung","Martin","Jackson","Thompson","White","Lopez","Sato"
$UserBaseOU = "OU=User Accounts,"
$UserOUs = "AMERS", "EMEA","APJ"
#$currentErrorPref = $ErrorActionPreference
#$ErrorActionPreference = "SilentlyContinue"
New-ADOrganizationalUnit -Name ((Get-AdDomain).Name + "corp") -Path (Get-AdDomain).DistinguishedName
ForEach($OU in $BaseOUs){
Write-Host "Adding $($OU) to $($TopOU)"
New-ADOrganizationalUnit -Name $OU -Path $TopOU
}
ForEach($OU in $UserOUs){
Write-Host "Adding $($OU) to $($UserBaseOU + $TopOU)"
New-ADOrganizationalUnit -Name $OU -Path ($UserBaseOU + $TopOU)
}
$i = 1
$f = 0
$l = 0
$o = 0
Write-Host "Creating $($NumberOfUsers) users..."
#run until the number of accounts provided via the numberofusers param are created
do
{
$fname = $FNames[$f]
$lname = $LNames[$l]
$UserOU = "OU=" + $UserOUs[$o] + "," + $UserBaseOU + $TopOU
$samAccountName = $fname.Substring(0,1)+$lname+$i
$password = ConvertTo-SecureString B4dp4ss4me -AsPlainText -Force
$name = $fname + " " + $lname
Write-Host "Adding User $($samAccountName) $($name) to $($UserOU)"
New-ADUser -SamAccountName $samAccountName -Name $name -GivenName $fname -Surname $lname -AccountPassword $password -Path $UserOU -Enabled $true -ErrorAction SilentlyContinue -ErrorVariable err
$f++
$l++
$o++
if ($f -eq $FNames.count) {$f = 0}
if ($l -eq $LNames.count) {$l = 0}
if ($o -eq $UserOUs.count) {$o = 0}
$i++
}
#run until numberofusers are created
while ($i -le $NumberOfUsers)
#set erroractionprefs back to what they were
#$ErrorActionPreference = $currentErrorPref
}
Create-UserList -NumberOfUsers 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment