It seems like I’m always building demo environments, whether it be for personal demo environments for speaking engagements or developer VMs for project work. One constant (other than installing SharePoint, SQL, Visual Studio, SharePoint Designer, etc.) is that I’m always trying to come up with names for users in Active Directory so I can run the User Profile Sync and show some data.
After doing this about 1,000,000 times – I decided to use something I learned at PowerShell Saturday over the weekend, the AD Cmdlets! There is a New-ADUser cmdlet that provisions an Active Directory user.
Once I knew that, I thought “I bet I could provide an array of names and use Get-Random to create users with random first names and last names.” That’s exactly what I set out to do, and I’ve got it working – it essentially uses a do while loop to run until the number of users specified is reached. I’ve looked up popular names to create a simple array of a few dozen first names and 20 or so last names. Get-Random simply creates random names from these arrays. Without further ado, here’s the function!
function Create-RandomADUsers { <# .SYNOPSIS This function provisions random AD users. .DESCRIPTION This function uses Get-Random and New-ADUser to provision users with random names. .PARAMETER NumberOfUsers The number of users to provision. .PARAMETER Path The Active Directory OU to store the users. .EXAMPLE PS C:\>Create-RandomADUsers -NumberOfUsers 50 -Path "OU=Demo Users,DC=contoso,DC=com" .LINK www.sharepointryan.com http://twitter.com/sharepointryan #> Param( [int]$NumberOfUsers, $Path ) #import the AD module Import-Module ActiveDirectory -ErrorAction SilentlyContinue #top first names and last names of 2011 $FirstNames = "Jacob","Isabella","Ethan","Sophia","Michael","Emma","Jayden","Olivia","William","Ava","Alexander","Emily","Noah","Abigail","Daniel","Madison","Aiden","Chloe","Anthony","Mia","Ryan","Gregory","Kyle","Deron","Josey","Joseph","Kevin","Robert","Michelle","Mandi","Amanda","Ella" $LastNames = "Smith","Johnson","Williams","Jones","Brown","Davis","Miller","Wilson","Moore","Taylor","Anderson","Thomas","Jackson","White","Harris","Martin","Thompson","Garcia","Martinez","Robinson","Clark","Rodriguez","Lewis","Lee","Dennis" #get current error preferences $currentErrorPref = $ErrorActionPreference #set error preferences to silentlycontinue $ErrorActionPreference = "SilentlyContinue" #start at 1 $i = 1 #tell us what it's doing Write-Host "Creating $($NumberOfUsers) users..." #run until the number of accounts provided via the numberofusers param are created do { $fname = $FirstNames | Get-Random $lname = $LastNames | Get-Random $samAccountName = $fname.Substring(0,1)+$lname $password = ConvertTo-SecureString p@ssw0rd -AsPlainText -Force $name = $fname+ " " + $lname $description = $password New-ADUser -SamAccountName $samAccountName -Name $name -GivenName $fname -Surname $lname -AccountPassword $password -Description "p@ssw0rd" -Path $path -Enabled $true -ErrorAction SilentlyContinue -ErrorVariable err $i++ } #run until numberofusers are created while ($i -le $NumberOfUsers) #set erroractionprefs back to what they were $ErrorActionPreference = $currentErrorPref }
This is a handly little script. I think I’ll use this in my lab. Thanks for sharing Ryan.
I am wondering a couple of things. Is there a maximum number of users this can create? What happens if there is a duplicate first and last name combination from the Get-Random?
Thanks Bob!
Currently I’ve got no logic or error-handling in place to limit the number of users, and I’m also just ignoring any errors which could occur from trying to create duplicate entries.
I’m actually working on that, and once I get it right I will probably update this post.
Thanks!
Ryan
Fantastic script – thanks ! Just getting a SharePoint demo – and needed some users for user profile, etc. This took me less than 5 mins. Cheers…!
Glad it was helpful! Thanks for taking a minute to comment, much appreciated!
RD