What are the situations you’ll need lots of Dummy AD data? When you want to run some awesomely crafted PowerShell AD scripts, that’s where.

I was in a situation a few months ago where I needed replicate a VERY large set of AD data in order to pull some queries with Get-Aduser and Set-Aduser. I toyed with the idea of just creating them manually, or restoring them from production (this is a bad idea, don’t restore from production for a testing environment!). So I found a script that would import a great deal of AD data, and I also found a place online that can create a buttload of fake AD data for me.

The Setup:

First, let’s hit mockaroo.com.

For this entry, I’m going to fill as many fields as possible that relate to a powershell script. For each field in powershell, we’ll create a matching one in Mockaroo, and we’ll name it consistently.

This is the set of data with parameters I’m using:

The fields I’m using in AD are (copied from above)

  • City
  • Company
  • Description
  • Department
  • Email
  • EmployeeID
  • GivenName
  • MobilePhone
  • Office
  • OfficePhone
  • Password
  • Path
  • Postalcode
  • State
  • Streetaddress
  • Surname
  • Title

I also need to place these created accounts into a specific OU. I had to do some editing in Excel, and placed the OU ADSI path into that Path attribute above.

Here’s my example with column and headers:

CityCompanyDepartmentEmailEmployeeIDGivenNameMobilePhoneOfficeOfficePhonePasswordPathPostalcodeStateStreetAddressSurNameTitle
IpabaSkybleHuman Resourcescyouthead0@google.es873-02-1259Codie+55 283 153 2678 592-719-1607MhyWOJIAE“OU=Contoso-Users,DC=Contoso,DC=com”35198-000 787 Arizona TrailYoutheadStructural Analysis Engineer

Generate the data you need, or create the information you require and save it into a CSV file (ideally in a Windows format).

Now, the powershell code:

# Import active directory module for running AD cmdlets
Import-Module activedirectory

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv "C:\Users\Administrator\Desktop\2AD_users.csv"

#Loop through each row containing user details in the CSV file 
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a variable as below
$City			=$User.City
$Company		=$User.Company
$Description	=$User.Description
$Department		=$User.Department
$Email			=$User.Email
$Employeeid		=$User.Employeeid
$GivenName		=$User.GivenName
$MobilePhone	=$User.MobilePhone
$Office			=$User.Office
$OfficePhone	=$User.OfficePhone
$Password		=$User.Password
$Path			=$User.Path
$Postalcode		=$User.Postalcode
$State			=$User.State
$Streetaddress	=$User.Streetaddress
$SurName		=$User.SurName
$Title			=$User.Title
$Username		=$GivenName+"."+$Surname

    #Check to see if the user already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username})
    {    #If user does exist, give a warning
		 Write-Warning "A user account with username $Username already exist in AD."
	}
	else
    {
        #User does not exist then proceed to create the new user account
		#Account will be created in the OU provided by the $Path variable read from the CSV file
        New-ADUser `
            -City $City `
            -Company $Company `
            -Description $Description `
            -Department $Department `
            -Email $Email `
            -EmployeeID $EmployeeID `
            -GivenName $GivenName `
            -MobilePhone $MobilePhone `
            -Office $Office `
            -OfficePhone $OfficePhone `
            -AccountPassword (convertto-securestring $Password -AsPlainText -Force) `
            -Path $Path `
            -Postalcode $Postalcode `
            -State $State `
            -Streetaddress $Streetaddress `
            -SurName $SurName `
            -Title $Title `
            -SamAccountName $Username `
            -UserPrincipalName "$Username@contoso.com" `
            -Name "$GivenName $SurName" `
            -DisplayName "$GivenName $SurName" `
            -Enabled $true
    }
}

I had to reference the New-ADUser powershell commandlet. A great deal of the switches came from this article. As you can see, I’ve also alphabetized the fields to make sorting a little easier.Notes about this script:

Code Syntax explanation – lines 11 – 28

$City			=$User.City
$Company		=$User.Company
$Description	=$User.Description
$Department		=$User.Department
$Email			=$User.Email
$Employeeid		=$User.Employeeid
$GivenName		=$User.GivenName
$MobilePhone	=$User.MobilePhone
$Office			=$User.Office
$OfficePhone	=$User.OfficePhone
$Password		=$User.Password
$Path			=$User.Path
$Postalcode		=$User.Postalcode
$State			=$User.State
$Streetaddress	=$User.Streetaddress
$SurName		=$User.SurName
$Title			=$User.Title
$Username		=$GivenName+"."+$Surname

This code stores the CSV columns (headers) information for each user into $variables. I’ve kept the variable names the same as the CSV headers tokeep things simple. The last line, $username stores the firstname (period) lastname. Everyone will likely have a different standard for usernames, I just like this format since I don’t have to re-write code.

Code Syntax explanation – lines 31-33

if (Get-ADUser -F {SamAccountName -eq $Username})
    {    #If user does exist, give a warning
		 Write-Warning "A user account with username $Username already exist in AD."

Some error checking code. Checking for duplicate information in the imported CSV file.

The variables used in this script are considered ‘more than necessary’. The bare basic amount needed (I think) are username, password, first and last name. Anything you add or take away, requires some editing of the code, and the CSV file.

Notes about this Script:

  • For some reason the New-ADUser command does not allow the -country variable, it seems to fail each time. This is not a deal breaker for me, but for you it might be something worth investigating.
  • When importing passwords, use 10 characters at a minimum. mockaroo.com only shows a password “between 6 to 12 chars”. By trial and error, I found manytools.org where I could generate the passwords necessary in the format I wanted, and copy/pasted them into my user CSV.
  • Should you feel hesitant about running this script, add “-Whatif” on the end of the Add-ADUser cmdlet line