Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This function creates AD users intended for lab use.
[Parameter(Position=0, Mandatory=$false)]
$count = 1,
[Parameter(Position=2, Mandatory=$true)]
[Parameter(Position=3, Mandatory=$false)]
$UpnSuffix = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().RootDomain.Name,
[Parameter(Position=4, Mandatory=$false)]
$OrganizationalUnit = ("CN=users," + ([ADSI]"LDAP://RootDSE").defaultNamingContext)
$webClient = New-Object System.Net.WebClient
$user = $webClient.DownloadString('')
$users = $user | ConvertFrom-Csv
$userpwd = ConvertTo-SecureString -AsPlainText $password -Force
1..$count | %{
$r1 = Get-Random -Min 1 -Maximum 1000
$r2 = Get-Random -Min 1 -Maximum 1000
$firstname = $users[$r1].firstname
$lastname = $users[$r2].lastname
$upn = "$($firstname[0])$lastname@$UpnSuffix".ToLower()
$name = "$firstname $lastname"
$alias = "$($firstname[0])$lastname".ToLower()
New-ADUser -Name $name `
-DisplayName $name `
-GivenName $firstname `
-Surname $lastname `
-Description $name `
-Office 'Azure Lab' `
-UserPrincipalName $upn `
-SamAccountName $alias `
-AccountPassword $userpwd `
-ChangePasswordAtLogon $false `
-Path $OrganizationalUnit `
-EmailAddress $upn `
-Enabled $true `
-PasswordNeverExpires $true `
This function creates AD users intended for lab use.
This function generates random user names for AD users using a CSV file.
The CSV file used to provide the names contains 1000 unique, commonly used,
first and last names provided by the US Census Bureau.
Specifies the number of users to create. The default value is 1.
The password for the Active Directory account.
The UPN suffix for the Active Directory user. If not provided, the Active
Directory root domain name will be used.
.PARAMETER OrganizationalUnit
Specifies the OU for the Active Directory account. If no value is provided,
the account will be created in the default users container.
New-LabUser -Count 5 -Password P@ssw0rd2016
Creates 5 AD users using random user names.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment