Skip to content

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

Tag: New-ADUser

Creating Random Demo Users in Active Directory using PowerShell

March 14, 20126 Comments

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
}

Share:

  • Tweet

Like this:

Like Loading...

Most Popular Posts

  • Remove SharePoint Groups using PowerShell
  • Retrieve SharePoint Groups using PowerShell
  • Delete all SharePoint List Items using PowerShell

Tag Cloud

Access Denied August 2011 CU Bulk Page Creation Bulk Site Creation content organizer Content Types Convert to Lowercase Correlation ID Create SharePoint Quota Template using PowerShell CSOM CSS Custom 404 DAYSPUG Document Management Dog Food Conference ECM Fileshare ForEach-Object Generic nonhelpful errors Get-Random Get-SPGroup Get-SPSite Get-SPWebApplication Heartland Region SharePoint Conference InfoPath InfoPath 2010 List Items List Settings Managed Metadata Master Page Microsoft.SharePoint.PowerShell Microsoft Heartland Region New-SPGroup New-SPPage New-SPWebFromXml Object Model Office 365 PowerShell PowerShell;SharePoint 2010; New-SPList;SP2010 Object Model PowerShell Saturday 002 Preserve Metadata Print JavaScript Print List View Print SharePoint Page Publishing Pages Quota Templates Records Management Remove-SPGroup REST Service Pack 1 Set-SPCustomLayoutsPage Set-SPPortalSiteUrl Setup-SPWebAppAndSite SharePoint SharePoint 2010 SharePoint 2013 SharePoint Automation SharePoint List SharePoint Online SharePoint Online PowerShell SharePoint Saturday Site Collection Site Collection Templates SP2013 Speaking Engagements SPQuotaTemplate SPSCincinnati SPSDayton SPSite SPWeb Subsite toLower() user adoption Workflow XML

Recent Tweets

  • RT @DanPriceSeattle: Let me see if I have this right: We couldn't afford $2,000 checks because we spend $740 billion a year on Defense, ye… 2 weeks ago
  • RT @davidmweissman: .@jack, deactivate Trump's Twitter account now please. 2 weeks ago
  • RT @stephenfhayes: President Trump needs to be impeached and removed, immediately. And responsible Republicans need to lead the effort. 2 weeks ago
  • RT @MSFT365Status: We're investigating an issue affecting access to multiple Microsoft 365 services. We're working to identify the full imp… 3 months ago
  • Since I won’t be attending a live rock show anytime soon, I’m really digging my new @nothingmorerock… twitter.com/i/web/status/1… 9 months ago
Follow @SharePointRyan

Facebook

Facebook

RSS

  • RSS - Posts
  • RSS - Comments

Blog Stats

  • 461,995 hits

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

Footnotes

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.com
Blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
%d bloggers like this: