Automated Random User Creation Script for Active Directory

If you're working with Active Directory (AD) and need to create multiple users for a reason or another, this script can help you automate the process. This script creates a specified number of random users...

· 2 min read
Automated Random User Creation Script for Active Directory

If you're working with Active Directory (AD) and need to create multiple users for a reason or another, this script can help you automate the process. This script creates a specified number of random users in an Organizational Unit (OU) and assigns them random first and last names while generating a username based on those names. It also sets a common password for all users.

Prerequisites:

  • PowerShell version 5.1 or later
  • Active Directory module installed

Instructions:

  1. Open PowerShell as an administrator.
  2. Navigate to the directory where you want to save the script.
  3. Create a new .ps1 file and paste the script code into it. Save the file with a meaningful name, such as CreateUsers.ps1.
  4. Modify the following variables according to your requirements:
    • $OUPath: The OU path where you want to create users (e.g., "OU=YourOU,DC=example,DC=com").
    • $NumberOfUsers: The number of users you want to create (e.g., 10).
  5. Run the script using the command .\CreateUsers.ps1.
# STH
# Define the OU where you want to create users
$OUPath = "OU=YourOU,DC=example,DC=com"

# Number of users to create
$NumberOfUsers = 10

# Function to generate random names
function Get-RandomName {
    $FirstName = Get-Random -InputObject @("John", "Jane", "Michael", "Sarah", "David", "Emily")
    $LastName = Get-Random -InputObject @("Smith", "Johnson", "Brown", "Davis", "Miller", "Wilson")
    $FullName = "$FirstName $LastName"
    return $FirstName, $LastName
}

# Loop to create users
for ($i = 1; $i -le $NumberOfUsers; $i++) {
    $FirstName, $LastName = Get-RandomName
    $UserName = "$FirstName$LastName"  # You can customize the username generation logic
    $UserPassword = "P@ssw0rd"  # You can set a common password here or generate random ones
    
    New-ADUser -Name $UserName -GivenName $FirstName -Surname $LastName -UserPrincipalName "$UserName@example.com" -Path $OUPath -AccountPassword (ConvertTo-SecureString $UserPassword -AsPlainText -Force) -Enabled $true
    Write-Host "Created user: $UserName"
}

Script Explanation:

  • Line 1-2: Define the OU path and the number of users to create.
  • Lines 4-7: Define a function called Get-RandomName that generates random first and last names using predefined lists.
  • Lines 9-13: Loop through the specified number of users, generating random names and usernames, as well as setting a common password for all users.
  • Line 14: Use the New-ADUser cmdlet to create a new user in the specified OU with the generated information. The -AccountPassword parameter converts the plaintext password to a secure string format.
  • Line 15: Display a message indicating that the user has been created successfully.

Notes:

  • This script requires an Active Directory module and appropriate permissions for creating users in the specified OU.
  • The $FirstName and $LastName variables are used to generate unique usernames (e.g., "JaneSmith"). You can modify this logic if you prefer a different username format.
  • All users are assigned the same password ("P@ssw0rd"). For better security, consider implementing random password generation or using an existing password management system.