Script to populate Active Directory with test accounts
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm
Probably like many of you, I quite often need to create test environment for dealing with a range of test scenarios. Invariably, when building a test environment, a question comes up on how to populate Active Directory with test accounts to simulate production environment. Sometimes you may be able to perform restore from production environment or get an export from HR or other systems. But in many cases (for example performance testing, product functionality testing, backup and recovery etc) you don’t really need to have an exact replica of your production environment, so it would be very useful to have a script that could generate a simple but realistic Active Directory environment based on some random names.
Luckily, there is script available on Microsoft scripting repository site that provides us with a starting point http://www.microsoft.com/technet/scriptcenter/scripts/ad/users/manage/usmgvb03.mspx?mfr=true
This script will generate an arbitrary number of accounts in a specified OU based on a very simple naming convention (User1, User2, … UserN).
Despite the fact that this is a good starting point, the script has some limitations:
- Does not distribute users across OUs
- The names are not realistic. By the way, why having realistic names is important? Well, if you doing tests on conducting automated user management, sometimes real world names will cause some issues if you don’t catch them in the test. For example, would you process properly handle names with spaces in them, or accented characters?
Finally, I decided to invest some time and enhance the script to overcome those limitations. The script that I provide here takes as an input 3 Excel Spreadsheets: first names list, last names list, and OUs list. As you may have guessed, the first names list provides a list of sample first names and the last names list provides a list of sample last names. I provide 2 Excel spreadsheets with European names; feel free to modify those to fit your locale. The script creates a name based on selecting random values from the list of first and last names. The samAccountName is generated based on first letter of first name and full last name. Despite using random combination of names, the script quite often may produce identical names which would cause errors while creating AD accounts. In this script I simply ignore those errors and let the script try again.
The third Excel spreadsheet has a list of Active Directory OUs names (fully distinguished names) among which to distribute the users (sample provided below)
Before running the script, modify this portion of it according to your environment:
REM Modify these values according to your environment
excelPathFirstNames = "c:\dbpopulate\FirstNamesEurope.xls"
excelPathLastNames = "c:\dbpopulate\LastNamesEurope.xls"
excelPathOUs = "c:\dbpopulate\ou.xls"
upnSuffix = "contoso.com"
count = 30000
Set objRootDSE = GetObject("LDAP://rootDSE")
By the way, since the script uses Excel files as an input make sure to have Excel installed on the machine from which you are executing the script.
That is it; I hope that you may find this script useful.
The script and accompanying sample files can be found in this blog attachment below.