Create random test users in a domain with PowerShell

Keeping up with the PowerShell posts!

This is a quick one I’ve created based on another one I once found to make it faster for me to create a number of test users when doing demos/labs.
It uses a .csv file as input with some 400 first and lastnames, which then is used for generating random usernames. (3+3 letters and incrementing number if needed. Based on a solution that can be done in many ways… )
We are also picking departments from the array $Departments (if you have the luck of getting a extra nice firstname, you get another department)

The only thing you need to do in order to get it working in your own lab or test domain (except for the script and the .csv file), is to insert the an OU distinguishedName to the $OU variable.

It will by default create 100 users, but that can be changed to a number of your choice with the -Numusers parameter.

2014-01-08 14-43-32
Download the script and the csv file here

CreateTestADUsers.ps1

<#
.SYNOPSIS
    The Script creates test users to your demo domain based on first and last namnes from csv. 
.PARAMETER NumUsers
    Integer - number of users to create, default 100.
.NOTES
    File Name: CreateTestADUsers.ps1
    Author   : Johan Dahlbom, johan[at]dahlbom.eu
    Blog     : 365lab.net
    The script are provided “AS IS” with no guarantees, no warranties, and they confer no rights.    
#>
param ([parameter(Mandatory=$false)]
[int]
$NumUsers = "100"
)
#Define variables
$OU = "OU=TestUsers,OU=Cloud Inc.,DC=cloud,DC=lab"
$Departments = @("IT","Finance","Logistics","Sourcing","Human Resources")
$Names = Import-CSV FirstLastEurope.csv
$firstnames = $Names.Firstname
$lastnames = $Names.Lastname
$Password = "Password1"

#Import required module ActiveDirectory
try{
Import-Module ActiveDirectory -ErrorAction Stop
}
catch{
throw "Module ActiveDirectory not Installed"
}

while ($NumUsers -gt 0)
{
      #Choose a 'random' department Firstname and Lastname
      $firstname = $FirstNames | Get-Random
      $lastname = $LastNames | Get-Random

      if (($firstname -eq "Johan") -or ($firstname -eq  "Andreas")) {
                $Department = "Tailspintoys - 365lab.net"
            } else {
                $Department = $Departments | Get-Random
            }
      #Generate username and check for duplicates

      $username = $firstname.Substring(0,3).tolower() + $lastname.Substring(0,3).tolower()
      $exit = 0
      $count = 1
        do
        {
             try {
                 $userexists = Get-AdUser -Identity $username
                 $username = $firstname.Substring(0,3).tolower() + $lastname.Substring(0,3).tolower() + $count++
             }
             catch {
                 $exit = 1
             }
         }
        while ($exit -eq 0)

      #Set Displayname and UserPrincipalNBame
      $displayname = $firstname + " " + $lastname
      $upn = $username + "@" + (get-addomain).DNSRoot

      #Create the user
      Write-Host "Creating user $username in $ou"
      New-ADUser –Name $displayname –DisplayName $displayname `
                 –SamAccountName $username -UserPrincipalName $upn `
                 -GivenName $firstname -Surname $lastname -description "Test User" `
                 -Path $ou –Enabled $true –ChangePasswordAtLogon $false -Department $Department `
                 -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -force)

      $NumUsers--
}

Feel free to edit or change as you wish!

/Johan

2 thoughts on “Create random test users in a domain with PowerShell

  1. Matthew Cole

    Change line 66 to use the $username for -Name rather than $displayname to avoid collisions. In AD the Name property is used to form the DN and that needs to be unique just like the SamAccountName does…if you are using this to hydrate many users into the same OU then the DN will already exist in some cases unless you use the unique $username as the -Name.

    Reply

Leave a comment