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

by Andy Grogan on December 3, 2009 · 29 comments

in Powershell, Quick Tips, Test Labs

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.

{ 26 comments… read them below or add one }

Mathias June 17, 2011 at 12:18 pm

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!

Reply

Andy Grogan June 19, 2011 at 9:20 am

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?

Reply

Unais July 15, 2011 at 1:02 pm

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”,”$samAccountName@$domain”)
$LABUser.Put(“description”, “Lab User – created via Script”)
$LABUser.Put(“userPrincipalName”,”$samAccountName@$domain”)
$LABUser.SetInfo()

Thanks
Unais
unaischr@gmail.com

Reply

Tunde July 25, 2011 at 3:52 pm

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

Reply

Andy Grogan January 12, 2013 at 9:12 am

Hiya Tunde, there is an update to the script that does exactly that located here: http://www.telnetport25.com/2011/12/update-to-my-create-lab-users-powershell-scriptmultiple-domains-and-500-more-users/

Cheers
A

Reply

Grady Nelson January 27, 2012 at 12:16 am

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

Reply

biplob January 30, 2012 at 10:45 am

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

Reply

Andy Grogan January 30, 2012 at 1:39 pm

What is your Powershell execution policy set to? You should set it to “RemoteSigned”

Reply

Rupesh January 11, 2013 at 10:12 pm

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”,”$samAccountName@$domain”)
$LABUser.Put(“description”, “Lab User – created via Script”)
$LABUser.Put(“userPrincipalName”,”$samAccountName@$domain”)
$LABUser.SetInfo()

$Pwrd = $User.Password

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

Reply

Andy Grogan January 12, 2013 at 9:08 am

Rupesh, what error message are you getting when you run the script?
Thanks

Reply

Rupesh January 14, 2013 at 1:55 pm

When i run the command, below message is appear

creating user: sAMaccountName
Script completed

Regards
Rupesh

Reply

Rupesh January 14, 2013 at 7:17 pm

the script run successfully.

wrong spell in the row.

regard
rupesh

Reply

MArk Goodwin February 4, 2013 at 9:05 am

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

I want to import 3000 accounts across multiple databases.

thanks

Reply

uday arun February 28, 2013 at 11:13 am

hey,i am new to scripting.i executed the script it successfully executed but no OU was being created.please help me

Reply

uday arun February 28, 2013 at 1:33 pm

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

Reply

uday arun February 28, 2013 at 1:36 pm

nope

Reply

uday arun February 28, 2013 at 1:37 pm

no i didn’t even produce any errors

Reply

uday arun February 28, 2013 at 1:56 pm

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

Reply

Lamont March 5, 2013 at 4:21 am

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

Reply

Lamont March 5, 2013 at 5:25 am

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

Reply

Andy Grogan March 5, 2013 at 5:35 pm

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

Reply

Bejar August 28, 2013 at 4:58 pm

Hi, my script runing succsefully, bout i did not get any useres in the AD ? some one can help me please

Reply

Scott October 15, 2013 at 2:31 pm

Thanks Bud! Very much appreciated.

Reply

ak47 February 22, 2014 at 4:49 pm

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

Reply

clash of clans hack ipad May 19, 2014 at 7:49 pm

. . som med den anden kategorier , denne beskrivelse er mere af en tese end
en regel

Reply

Bassam June 29, 2014 at 9:21 am

How we can set the Powershell execution policy to “RemoteSigned”

Reply

Leave a Comment

*

{ 3 trackbacks }

Previous post:

Next post: