Creating random users for a domain

I needed to create some users for my Active Directory domain I created to use in the book “Windows PowerShell Bible” for the Exchange Management chapter.

I could have done something simple like:

1..120 | ForEach { Net User “BookUser$_” P@ssW0rd /ADD /Domain}

This would have created 120 users in the Users ou on my domain, but they would have all been “BookUser” users – where’s the variety in that?

I wanted random users, to more closely mimic a real domain. Searching for a random name generator led me to I chose to create 30 random names for both sexes, and ran the “Generate Random Names”

I copied the 30 resultant names to a notepad, and ran the page two more times, giving me 120 names.

Of those, a few were single names, which I eliminated.

I then replaced each space with a comma, saved the file as a .csv, and opened it in Excel.

Using Excel, I created a user name consisting of the first initial of the first name, and the whole last name.

I then added a 4th column, for the password.

Finally, I added column headers: First, Last, Login, and Password. Once again, I saved the file.

Now, I’m ready for PowerShell:

$bookOU = [ADSI]“LDAP://OU=Users,OU=Apps,DC=book,DC=local” 
foreach ($user in import-csv users.csv

$newUser = $bookOU.Create(“user”,“cn=” +$user.login)
$newUser.put(“sn”, $user.Last)
$newUser.put(“DisplayName”, $user.First + ” “ +$user.Last)
$newUser.put(“givenName”, $user.First)
$newUser.put(“userPrincipalName”,$user.login + “”)
$newUser.put(“userAccountControl”, 512)

There you go… 14 lines, and a little work, and I have over 100 users on my domain.

Edited as I realized I needed the samaccountname and userprincipalname, and to follow Doug’s suggestion,

  1. #1 by Doug on October 12, 2010 - 12:32

    Hey Karl,

    Just curious.

    I prefer this:

    foreach($user in (import-csv users.csv)) {

    One less variable. Less memory consumption. Readability is interesting.

    Any thoughts?

    • #2 by karlmitschke on October 15, 2010 - 02:13

      Thanks, Doug,

      I modified the script.


  2. #3 by karlmitschke on October 12, 2010 - 12:49

    I go back and forth on that one. When I remember, I do it :)

    I thought about doing:
    Import-Csv users.csv |Foreach-Object{

    as well…


  3. #4 by Tony on February 15, 2012 - 01:49

    Very impressed, can you post the csv ?
    My csv format is something like this:

    but it doesn’t seem to create anything on AD.


    • #5 by Karl Mitschke on February 22, 2012 - 11:23

      I’m sorry, that was well over a year ago – I no longer have the .csv
      What happens if you run this?
      foreach ($user in import-csv users.csv){
      Write-Host $user.login
      Write-Host $user.Last
      Write-Host $user.First
      Write-Host $user.password

  4. #6 by Mike on March 21, 2012 - 12:55

    You do not need a csv file to do this, check out the VB script method here: and grab document #8 on the page. You can create as many random users as you would like. super easy.

    • #7 by Karl Mitschke on March 23, 2012 - 08:16

      That creates a bunch of users named TestUser1 – 200.
      If you’d read my post, you’d know I did not want that, but could do it in 1 line in PowerShell ;)


  1. Tweets that mention Creating random users for a domain « Unlock-PowerShell --

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 74 other followers

%d bloggers like this: