共用方式為


PS without BS: Creating Random Test Users in Active Directory

This was an interesting ask, and kind of showcases a couple of different techniques. The ask was to create 20 random users in Active Directory for test purposes.

So, I grabbed a list of (debateably) a list of the most common male and female first names and the most common surnames. I then randomize these into "fake/real names" with user names of userxx, whereas xx is relative to the position of the user. This is a sample of what comes out (over 5 users):

Ruth Robinson (user1)
Richard Harris (user2)
Sandra Johnson (user3)
Christopher Lee (user4)
Susan Martinez (user5)

Script features:

  • Allows you to create as many or few users as you want (keep in mind there are so many name combinations, so add more if you want.
  • Which OU to put the user accounts in (the Distinguished Name, make sure it exists first)
  • The default user password
  • The UPN suffix for the user account, if you like to login as user1@domain.local vs. DOMAIN\User1
  • Whether or not the user account is enabled
  • Whether or not the user is forced to change password when they log in.
  • Whether or not the user's password expires.

Below is a screen shot of AD Users and Computers and the user properties that are relevant.

Personally, I like the idea of just setting a basic password, not expire it, and and not forcing unique passwords since it's my lab. Feel free to season these options to taste.

Below is the script, enjoy.

 
## Beginning of script - sample user creation written by Lee Stevens - https://aka.ms/leesteve

$arrayFirstName = "James John Robert Michael William David Richard Charles Joseph Thomas Christopher Daniel Paul Mark Donald George Kenneth Steven Edward Brian Mary Patricia Linda Barbara Elizabeth Jennifer Maria Susan Margaret Dorothy Lisa Nancy Karen Betty Helen Sandra Donna Carol Ruth Sharon".Split(" ")

$arrayLastName = "Smith Johnson Williams Jones Brown Davis Miller Wilson Moore Taylor Anderson Thomas Jackson White Harris Martin Thompson Garcia Martinez Robinson Clark Rodriguez Lewis Lee Walker Hall Allen Young Hernandez King".Split(" ")

## Variables to execute the script. Just change the lines within these comments. ##

# How many random users do you want to create
$intNumberUsers2Create=20

#What OU do you want to put the users in, use the DN"
#Also insure the OU exists
$strUserOU="OU=Users,DC=test,DC=local"

#The default password to assign the account
$strDefaultPassword="P@ssw0rd1"

#The UPN Suffix for the user accounts
$strUPNSuffix="test.local"

#Enable the user account?
$UserEnabled=$true

#Whether or not the user must change password on next logon
$UserMustChangePassword=$false

#Password never expires flag
$UserPasswordDoesntExpire=$true

## End of Variables ##
## Begin Main body of script ##
For ($i=1; $i -lt $intNumberUsers2Create+1; $i++) {

$strFirst = $arrayFirstName[ (Get-Random $arrayFirstName.count ) ]
$strLast = $arrayLastName[ (Get-Random $arrayLastName.count) ]
$strFullName = $strFirst+" "+$strLast

$strUserName="user"+$i
Write-Host $strFullName" ($strUserName)"

New-ADUser -Name $strFullName -DisplayName $strFullName -UserPrincipalName $strUserName@$strUPNSuffix -GivenName $strFirst -Surname $strLast -AccountPassword (ConvertTo-SecureString -AsPlainText $strDefaultPassword -Force) -Path $strUserOU -SamAccountName $strUserName -ChangePasswordAtLogon $UserMustChangePassword -PasswordNeverExpires $UserPasswordDoesntExpire -Enabled $UserEnabled
}

## End Script ##

— Easy link to my blog: https://aka.ms/leesteve
If you like my blogs, please share it on social media and/or leave a comment.