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:

001
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 http://www.kleimo.com/random/name.cfm. 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:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
$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(“sAMAccountName”,$user.login)
$newUser.put(“userPrincipalName”,$user.login + “@book.com”)
$newUser.SetInfo()
$newUser.SetPassword($user.password)
$newUser.put(“userAccountControl”, 512)
$newUser.SetInfo()
}

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,

Advertisement
  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.

      Karl

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

    Doug;
    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…

    Karl

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

    hi,
    Very impressed, can you post the csv ?
    My csv format is something like this:
    First,Last,Login,Password
    Kelly,Townson,townsonk,password
    Javier,Jeffords,jeffordsj,password
    Fernando,Memmott,memmottf,password

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

    Thanks
    Tony

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

      Tony;
      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: http://www.sysoptools.com/support.aspx?tbc=2 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 😉

      Karl

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

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

%d bloggers like this: