When testing Exchange Server configurations or developing directory-based applications, creating realistic test data is crucial. Manually adding 100 users through Active Directory Users and Computers (ADUC) would be time-consuming and error-prone. PowerShell provides the perfect automation solution.
# Import Active Directory module
Import-Module ActiveDirectory
# Define variables
$OUPath = "OU=TestUsers,DC=domain,DC=com"
$Password = ConvertTo-SecureString "P@ssw0rd1" -AsPlainText -Force
$Domain = "domain.com"
# Create 100 test users
1..100 | ForEach-Object {
$firstName = "TestUser"
$lastName = $_ # Using iteration number as unique identifier
$samAccountName = "testuser$_"
$upn = "$samAccountName@$Domain"
$displayName = "$firstName $lastName"
New-ADUser -Name $displayName
-GivenName $firstName
-Surname $lastName
-SamAccountName $samAccountName
-UserPrincipalName $upn
-Path $OUPath
-AccountPassword $Password
-Enabled $true
-ChangePasswordAtLogon $false
-EmailAddress "$samAccountName@$Domain"
}
For more realistic testing, we can generate random names using the NameIT
module:
# Install and import NameIT module
Install-Module -Name NameIT -Force
Import-Module NameIT
# Create users with random names
1..100 | ForEach-Object {
$randomName = Get-RandomName -FirstName -LastName
$firstName = $randomName.FirstName
$lastName = $randomName.LastName
$samAccountName = ($firstName.Substring(0,1) + $lastName).ToLower()
New-ADUser -Name "$firstName $lastName"
-GivenName $firstName
-Surname $lastName
-SamAccountName $samAccountName
-UserPrincipalName "$samAccountName@$Domain"
-Path $OUPath
-AccountPassword $Password
-Enabled $true
}
After running the script, verify the users were created successfully:
Get-ADUser -Filter * -SearchBase $OUPath |
Select-Object Name,SamAccountName |
Format-Table -AutoSize
For Exchange-specific testing, we can add mailbox attributes:
# After creating users, enable mailboxes
1..100 | ForEach-Object {
$samAccountName = "testuser$_"
Enable-Mailbox -Identity $samAccountName
}
When testing is complete, remove all test users:
Get-ADUser -Filter * -SearchBase $OUPath |
Remove-ADUser -Confirm:$false
When testing Exchange Server configurations, email routing, or group policies, having a substantial number of test users is crucial. Creating these manually would be time-consuming and impractical. Here's how to automate the process using PowerShell.
Before proceeding, ensure you have:
- Active Directory Module for PowerShell installed
- Appropriate permissions to create AD users
- Exchange Management Shell if creating mailboxes
Here's a complete script that creates 100 users with realistic names and assigns them to an OU:
# Import required module
Import-Module ActiveDirectory
# Define parameters
$OUPath = "OU=TestUsers,DC=domain,DC=com"
$Password = ConvertTo-SecureString "P@ssw0rd1" -AsPlainText -Force
$Domain = "@domain.com"
# Arrays of sample names
$FirstNames = @("James","John","Robert","Michael","William","David","Richard",
"Joseph","Thomas","Daniel","Matthew","Anthony","Donald","Mark",
"Paul","Steven","Andrew","Kenneth","Joshua","George","Kevin")
$LastNames = @("Smith","Johnson","Williams","Brown","Jones","Miller","Davis",
"Garcia","Rodriguez","Wilson","Martinez","Anderson","Taylor",
"Thomas","Hernandez","Moore","Martin","Jackson","Thompson","White")
# Create 100 users
1..100 | ForEach-Object {
# Generate random name
$FirstName = $FirstNames | Get-Random
$LastName = $LastNames | Get-Random
$SamAccountName = ($FirstName.Substring(0,1) + $LastName).ToLower()
# Create user properties
$UserParams = @{
GivenName = $FirstName
Surname = $LastName
Name = "$FirstName $LastName"
SamAccountName = $SamAccountName
UserPrincipalName = "$SamAccountName$Domain"
Path = $OUPath
AccountPassword = $Password
Enabled = $true
ChangePasswordAtLogon = $false
}
# Create the user
New-ADUser @UserParams
# Output progress
Write-Host "Created user: $SamAccountName"
}
To make the users more realistic and prepare them for Exchange testing:
# Add department and title
$Departments = @("Sales","Marketing","IT","HR","Finance","Operations")
$Titles = @("Manager","Specialist","Analyst","Director","Coordinator","Associate")
1..100 | ForEach-Object {
# ... previous code ...
# Add additional attributes
Set-ADUser $SamAccountName -Department ($Departments | Get-Random)
Set-ADUser $SamAccountName -Title "$($Titles | Get-Random)"
Set-ADUser $SamAccountName -Company "TestCorp"
# Create mailbox if Exchange is installed
if (Get-Command Enable-Mailbox -ErrorAction SilentlyContinue) {
Enable-Mailbox -Identity $SamAccountName
}
}
After running the script, verify the users were created:
Get-ADUser -Filter * -SearchBase $OUPath | Measure-Object
When testing is complete, remove all test users:
Get-ADUser -Filter * -SearchBase $OUPath | Remove-ADUser -Confirm:$false
For more complex testing scenarios, consider:
- Generating unique passwords for each user
- Creating distribution groups and adding test users
- Setting mailbox quotas and permissions
- Generating test emails between users