Quick Post – Script to Create Lab Users – Powershell version…

A long time ago I posted a VBSCRIPT and CSV file which when executed created over 300 user accounts in Active Directory which was designed to give you a number of test accounts which could be used within a LAB environment to test Exchange Server.
As mentioned the original script was based around VBSCRIPT and Exchange 2003 and also required a little bit of manual intervention in order for it to work within your specific environment.

Predominantly it was designed to work against Windows 2000 / 2003 server AD environments – however as time has elapsed its underling functionality has been superseded with the advent of Powershell / Windows 2008 and Exchange 2007 and 2010 to the point that under certain system configurations it will no longer work.

Therefore given the above (and that I have been rebuilding my test lab) I am pleased to present to you with an updated CSV file and script (where the script is now Powershell based) – which supports the following scenarios:

  • Can be executed via Powershell versions 1.0 and 2.0
  • Can be run on the following Operating Systems: Windows 2008 and Windows 2008 R2 (will also support Windows 2003 with Powershell installed)
  • The accounts created can be configured with mailboxes for Exchange 2007 and Exchange 2010

So, if you are creating a test lab which is based around the above technologies and would like an easy way to get over 300 unique accounts into your Active Directory environment please feel free to download the following file to either your LAB Domain Controller or Exchange Server:

ESEUtilGUIIcoCreate-Users.exe [ 142KB ]

The above file is a self extracting ZIP which when executed with extract two files to you Exchange Server / Windows 2008 Domain Controller.

When you have downloaded and extracted the scripts to your server – open a Powershell Console and navigate to the location where they are located – when there type in:

.\CreateUsersFromCSV.ps1 and then press – see below;

LabUsers01

When the script executes you will be presented with a Windows “Open” dialog box – navigate to the location where you extracted the files and then choose the sample CSV – see below;

LabUsers02

The script will then execute creating each user in your Active Directory Lab environment – the Default password is “Password1” – see below;

LabUsers03

When the script has completed – open Active Directory Users and Computers – you will see a new OU called “ExchangeUsers” – within this OU are the new accounts that you can play with – see below;

LabUsers04

I hope that you find the above useful.

Sharing is caring!:

29 thoughts to “Quick Post – Script to Create Lab Users – Powershell version…”

  1. I tried to use av csv file with columns instead av , separated but it didnt work. Is there a way to use a column seperated file?
    Its much easier to fill in the users then in Excel.

    Regards!

    1. Sorry Mathias, I am not sure I understand. When I construct the CSV file I use Excel and each of the properties are placed in columns. I then save the file as CSV. I am not sure what the problem is that you are describing?

  2. Dear Andy,

    Thanks for giving a wonderful script.
    I had a question, when i used the and created user account, i noticed a difference between manual and using script account creation that in Active directory users and groups, “Name ” column while user created using script will show “sanAccount name ” but user created manually in ADS will full name ( Fist name & Last Name – normally display name) so what’s wrong in script. Please advice me.
    Can you please provide me line by line discription of script, what’s $CN ,$SN

    $CN = $User.samAccountName
    $SN = $User.Surname
    $Given = $User.givenName
    $samAccountName = $User.samAccountName
    $Display = $User.DisplayName

    $LABUser = $UserPath.Create(“User”,”CN=$CN”)
    Write-Host “Creating User: $User.samAccountName”
    $LABUser.Put(“samAccountName”,$samAccountName)
    $LABUser.Put(“sn”,$SN)
    $LABUser.Put(“givenName”,$Given)
    $LABUser.Put(“displayName”,$Display)
    $LABUser.Put(“mail”,”[email protected]$domain”)
    $LABUser.Put(“description”, “Lab User – created via Script”)
    $LABUser.Put(“userPrincipalName”,”[email protected]$domain”)
    $LABUser.SetInfo()

    Thanks
    Unais
    [email protected]

  3. Hey Andy,
    Thank you for being such a life save when it comes to Exchange. I’ve read a lot of your articles.
    I have a question in regards to this scripts. The two lines below looks into the forest to get user account info:

    $domain = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
    $DomainDN = (([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).Domains | ? {$_.Name -eq $domain}).GetDirectoryEntry().distinguishedName

    This is great because it works just as described but is there anyway these two lines can be modified in case I have I forest with multiple child domains? When I ran the same script in a child domain, I got errors. I even tried to modify it to:
    $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
    $DomainDN = (([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).Domains | ? {$_.Name -eq $domain}).GetDirectoryEntry().distinguishedName
    but it didn’t work.

    I’m new to powershell. So, can you please help out?

    Tunde

  4. Thanks Andy,
    I was in the middle of Peter Bruzzese Exchange 2010 Traning he made bor Tainsignal. Peter had aleardy extracted fromt he MS Distribution disk all the files he need for Installation and others need for the course. I was used the MS Exchange 2010 DVD. Suddendly in discussion of the the Lab he goes to his “DVD” which is really on his C: driver and pull out you script. I scan the disk over and over again and could’t find it. He did mention that your name as the author of the Power Shell script but failed to mentione where to get it. I did a Google Search on “Andy Grogan” and found this web site. After prusing around a bit, I finally found this script. Tell Peter to get a URL cause he implied that it was in the MS distribution. It should be otherwise created the 300 users for the Course would have taken longer that the course. Anyway thanks for coming up with it. When I am ready to set up my production system, I use Excel and create my used in it and save the as a .csv file. Wala, all my users andd to the AD with one PS command. I’m stargin to lover PS and EPS but I’m an old DOS users anyway so it comes naturally.

    Again thanks for sharing,
    Grady Nelson
    XTC Media Group LLC
    Atlanta, GA
    Studio City, CA

  5. PS C:\> .\CreateLabUsers.ps1
    File C:\CreateLabUsers.ps1 cannot be loaded. The file C:\CreateLabUsers.ps1 is not digitally signed. The script will no
    t execute on the system. Please see “get-help about_signing” for more details..
    At line:1 char:21
    + .\CreateLabUsers.ps1 <<<<

  6. CSV file containing SAMaccount name is Firstname_last name.
    able to run script with firstname.lastname but it is not working with firstname_lastname.

    please let me know where i need to do changes in the script.

    $CN = $User.samAccountName
    $SN = $User.Surname
    $Given = $User.givenName
    $samAccountName = $User.samAccountName
    $Display = $User.DisplayName

    $LABUser = $UserPath.Create(“User”,”CN=$CN”)
    Write-Host “Creating User: $User.samAccountName”
    $LABUser.Put(“samAccountName”,$samAccountName)
    $LABUser.Put(“sn”,$SN)
    $LABUser.Put(“givenName”,$Given)
    $LABUser.Put(“displayName”,$Display)
    $LABUser.Put(“mail”,”[email protected]$domain”)
    $LABUser.Put(“description”, “Lab User – created via Script”)
    $LABUser.Put(“userPrincipalName”,”[email protected]$domain”)
    $LABUser.SetInfo()

    $Pwrd = $User.Password

    $LABUser.psbase.invoke(“setPassword”,$Pwrd)
    $LABUser.psbase.invokeSet(“AccountDisabled”,$False)
    $LABUser.psbase.CommitChanges()

  7. Good script Andy,
    How can this script be modified to select the mailbox database?

    I want to import 3000 accounts across multiple databases.

    thanks

  8. Andy,
    i tried even refreshing Active Directory Users and computers,but it’s not working can you please tell me something.

  9. thanks Andy,
    but it will take me a day’s time to get back to that server please provide me your mail id so that i can mail you the text file

  10. I am working on a project for for my exchange server class our instructor wants us to find a script online for creating multiple users and OUs through the script and yours appears to be able to do this with a little modification just not exactly sure where I need to modify the script at cause I have to create 3 ous with a set amount of users in each ou but its been a while since I scripted so I am kind of stuck at the moment as to where in your script I will need to edit it or if will even be able to set the script up to do what I need it to do. So far the simple way that I see would be to break the excel script into 3 different excel scripts with the set amount of users in each one but I am not sure how I need to set this up in your script

  11. ok after spending about an hour looking over your script I see what I need to do I just have 1 small issue I am not seeing in your script the location where you tell the script to pull the labusers.csv file cause I would just need to duplicate the OU creation and User creation in the script 2 more time and just define my OUs instead of the single OU you have and tell the script to look for my custom users lists in each section so it pulls the right about of users for each OU it creates.

    If you can point out the section where you have the script point to labusers.csv I can modify the script to fit my needs for my project other wise I will just have to set the script to create the default OU and modify down the labusers.csv to my set amount of users and manually create the other 2 OUs and move the users into the OUs which will be painful

    1. Lamont,

      The filename of the CSFIle is retrieved from the Select-FileDialog function.

      You will see this on line 37 ($Filename = Select-FileDialog…)

      The content of the CSVFile is then imported into a variable called $UserInformation – see line 48:

      $UserInformation = Import-CSV $Filename

      From what you are trying to do, you could stop using the Select-FileDialog function and do the following:

      $CSVFile1 = $CSVFile2 = $CSVFile3 =

      And then have:

      $UserInformation1 = Import-CSV $CSVFile1
      $UserInformation2 = Import-CSV $CSVFile2
      $UserInformation3 = Import-CSV $CSVFile3

      Thanks,

      Andy

  12. hi
    i have donwloaded the above file and extracted it and there is not ps script wtih the name indicated in the above screenshot . i.e. .\CreateUsersFromCSV.ps1 instead there is CreateLabUsers.ps1.

    but executing it gives the following error
    PS C:\Users\Administrator\Desktop\Create-Users> .\CreateLabUsers.ps1
    File C:\Users\Administrator\Desktop\Create-Users\CreateLabUsers.ps1 cannot be loaded because the execution of scripts i
    s disabled on this system. Please see “get-help about_signing” for more details.
    At line:1 char:21
    + .\CreateLabUsers.ps1 <<< ls

    Directory: C:\Users\Administrator\Desktop\Create-Users

    Mode LastWriteTime Length Name
    —- ————- —— —-
    d—- 2/22/2014 9:37 PM Create-Users
    -a— 2/22/2014 9:25 PM 159744 Create-Users.exe
    -a— 12/4/2011 2:56 PM 2550 CreateLabUsers.ps1
    -a— 2/22/2014 9:35 PM 5359 LabUsers.csv
    -a— 12/4/2011 2:54 PM 29159 NewUsers.csv

    PS C:\Users\Administrator\Desktop\Create-Users> .\CreateLabUsers.ps1
    File C:\Users\Administrator\Desktop\Create-Users\CreateLabUsers.ps1 cannot be loaded because the execution of scripts i
    s disabled on this system. Please see “get-help about_signing” for more details.
    At line:1 char:21
    + .\CreateLabUsers.ps1 <<<<
    + CategoryInfo : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

  13. hi
    i just left this message for those who after running this script are unabel to see users.

    first type below command in elevated powershell

    Set-ExecutionPolicy RemoteSigned

    then in elevated powershell run the script .. it will now create OU and users.

Leave a Reply

Your email address will not be published. Required fields are marked *