Tuesday, October 31, 2006
« Presention Slides and PowerShell code fr... | Main | SharePoint/PowerShell 3: Now add the use... »

Step 2 in our series is to add some users to Active Directory.

First we need a simple routine to add users to Active Directory

Update 3-11-2006 : The original add-aduser would only work in versions of PowerShell pre RC2. RC2 included some breaking changes to how the DirectoryEntry object handled. I;ve added an RC2 compatible routine.

Pre RC2 version


function add-aduser([string]$LoginName, [string]$DisplayName, [string]$FirstName, [string]$LastName)
{

    $cn=$LoginName
    $sam=$LoginName
    $pw="P@ssword1"


    $ad= new-object System.DirectoryServices.DirectoryEntry
    $u = $ad.get_Children().Find("CN=Users")
    $NewUser = $u.get_Children().add("CN=$cn",'User')

    
    $NewUser.InvokeSet("sAMAccountName",$sam)
    $NewUser.InvokeSet("displayName",$DisplayName)
    $NewUser.InvokeSet("FirstName",$FirstName)
    $NewUser.InvokeSet("LastName",$LastName)
    
    $NewUser.CommitChanges()

    $ad=new-object System.DirectoryServices.DirectoryEntry
    $u = $ad.get_Children().Find("CN=Users")

    $NewUser= $u.get_Children().Find("CN=$cn");
    $NewUser.Invoke("SetPassword",$pw)
    $NewUser.InvokeSet("AccountDisabled",$false)

    # set that the password never expires
    $NewUser.userAccountControl[0] = $NewUser.userAccountControl[0] -bor (65536)

    $NewUser.CommitChanges()

}

 

RC2 Version


function add-aduser([string]$LoginName, [string]$DisplayName, [string]$FirstName, [string]$LastName)

{

   $cn=$LoginName
   $sam=$LoginName
   $pw="P@ssword1"

   # Get an ADSI object for the default domain
   $ad= [ADSI]""

   # Get the Users OU as default
   $ou = $ad.psbase.Children.Find("CN=Users")

   # Add the user
   $NewUser = $ou.psbase.Children.Add("CN=$cn",'User') 

   # Set the basic properties
   $NewUser.Put("sAMAccountName",$sam)
   $NewUser.Put("displayName",$DisplayName)
   $NewUser.Put("givenname",$FirstName)
   $NewUser.Put("sn",$LastName)

   # Commit changes 
   $NewUser.SetInfo() 

   
   # Set our password 
   $NewUser.psbase.Invoke("SetPassword",$pw) 

   # And enable the account
   $NewUser.psbase.InvokeSet("AccountDisabled",$false

   # set that the password never expires
   $NewUser.userAccountControl[0] = $NewUser.userAccountControl[0] -bor (65536) 

   # Commit changes
   $NewUser.SetInfo()


}



This gets us a function we can call to add test users. Note we're hardcoding the OU to users, to make this more production you'd want to parameterize the OU, passwords and set the AccountControl flags to Change Password on next login, there's a lot of other resources out there to help with that.

So If we put that in a script file loaded from our Profile we can call it like this

add-aduser "joeb" "joe blogs" "joe" "blogs"

That's nice but with PowerShell we can do better. We really want to data drive this and the built-in import-csv function will parse a CSV and create a collection of objects that match the CSV schema.

Take a simple User's CSV file like this

LoginName, DisplayName, FirstName, LastName, Email

brianb, Brian Ballack, Brian, Ballack, brianb
walterf, Walter French, Walter, French, walterf


 

Now if we run an import-xml command on the CSV file this is the output

 

Each line has been parsed and an object created. To see this more clearly we can use the get-member command to reflect over the object and see what the object looks like.

 

(Note: it will be PSCustomObject not MshCustomObject in post RC0 builds , I'm running Exchange 2007 on this VPC which requires PowerShell RC0)

As you can see the CSV columns have been added as a NoteProperty property (this is part of the extensible type system).

So now we can create a really simple function to import a CSV file of users, pipe the output to a foreach loop and add each user to Active Directory.

# Function:         Import-Users
# Description:        Create users in active directory as listed in the import CSV file
# Parameters:        UserFile         Location of the CSV file containing the users
#
function Import-Users([string]$UserFile)
{
    Import-Csv $UserFile | foreach-object { add-aduser $_.LoginName $_.DisplayName $_.FirstName $_.LastName }
}

Note that when you receiving a collection of objects in the pipeline in a scriptblock you have to loop over each object. The $_ is a special variable that gives you the current object in the loop which you can use to access properties and methods.

To call we just do

Import-Users Users.CSV

With this power 500 users are as easy as 1 user.

We're not limited to just using CSV files, I've just picked that format as I'm always amazed at the amount of data I get given in Excel spreadsheets and word documents and CSV is the easiest intermediate format to work with. There is also an import-xml command which we will use later to import an XML file containing portal content. As Powershell can call any .Net library the System.Data set of classes could also be used to pull information from databases.

The next entry will be adding our users to SharePoint.

Tuesday, October 31, 2006 9:13:15 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [3]  |  Related posts:
UK Community Day at Microsoft Reading
PowerShell: Generating a proxy for all the SharePoint WebServices
Silverlight 2: Unable to call SharePoint's Web Services
Presenting SharePoint + Silverlight at the SharePoint UK UserGroup Basingstoke March 27th
Sharepoint and Powershell Virtual lab
SharePoint 2007 and Open Protocols
Tracked by:
"PowerShell 2: Add some Users to Active Directory, maybe 1, maybe 500 " (New! Ex... [Trackback]
http://blog.feelanet.com/ex2007/pages/SharePoint_2F00_PowerShell-2_3A00_-Add-som... [Pingback]
http://www.google.com/search?q=zramnkvc [Pingback]
http://www.eea-esem2006.org/fileadmin/b/ultram.htm [Pingback]
http://www.alpenhof.it/fileadmin/inc/d/best-prices-on-phentermine.htm [Pingback]
http://alsysinc.com/images/Image/a/cheap-meridia.htm [Pingback]
http://alsysinc.com/images/Image/a/lipitor-zocor.htm [Pingback]
http://shop.trovata.com/images/misc/2/somacruz.htm [Pingback]
http://egmsys.com/pmwiki/wiki.d/base/c/zyrtec.htm [Pingback]
http://tlcwe.com/cerberus-gui/templates_c/b/diet-pill-phentermine.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/c/order-viagra.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/a/xenical.htm [Pingback]
http://parmleyphotography.com/images/b/xenical.htm [Pingback]
http://parmleyphotography.com/images/c/diet-pill-phentermine.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/a/effexor.htm [Pingback]
http://sapid-club.com/soap/base/c/valium.htm [Pingback]
http://parmleyphotography.com/images/b/alprazolam.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/c/ambien.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/b/buycialisonline.htm [Pingback]
http://thebadd.org/mailcenter/users/ericp-thebadd.org/c/order-cialis.htm [Pingback]
http://tlcwe.com/cerberus-gui/templates_c/a/buy-propecia.htm [Pingback]
http://airport.by/drupal/files/b/buy-viagra-online.htm [Pingback]
http://tuttlemedia.com/images/main_page/a/propecia-prescription.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/a/levitra.htm [Pingback]
http://tlcwe.com/cerberus-gui/templates_c/a/prescription-drug.htm [Pingback]
http://thebadd.org/mailcenter/users/ericp-thebadd.org/a/xanax.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/d/paxil.htm [Pingback]
http://glamourshades.com/drupal/files/a/levitracialisviagracomparison.htm [Pingback]
http://sapid-club.com/soap/base/b/cheap-viagra.htm [Pingback]
http://airport.by/drupal/files/b/cheap-viagra.htm [Pingback]
http://tuttlemedia.com/images/main_page/d/diabetic-diet.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/b/buy-valium-online.htm [Pingback]
http://thebadd.org/mailcenter/users/ericp-thebadd.org/a/valium.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/d/diet-plan.htm [Pingback]
http://tlcwe.com/cerberus-gui/templates_c/b/weightlossprogram.htm [Pingback]
http://karenclarkphotography.com/clientgallery/d/hoodiadietpills.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/c/diet.htm [Pingback]
http://tuttlemedia.com/images/main_page/a/cheep-phentermine.htm [Pingback]
http://www.google.com/search?q=mtioxbif [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/b/buyviagraonline.htm [Pingback]
http://actionhouse.net/shop/c/levitra.htm [Pingback]
http://tlcwe.com/cerberus-gui/templates_c/a/buycarisoprodol.htm [Pingback]
http://glamourshades.com/drupal/files/d/walgreens-drug-store.htm [Pingback]
http://iseu.by/board/Packages/b/weightwatchers.htm [Pingback]
http://tuttlemedia.com/images/main_page/d/order-diazepam.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/b/tramadolonline.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/a/alprazolam.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/d/hoodia-diet-pills.htm [Pingback]
http://glamourshades.com/drupal/files/c/levitra-buy.htm [Pingback]
http://egmsys.com/pmwiki/wiki.d/base/d/buyonlinesoma.htm [Pingback]
http://egmsys.com/pmwiki/wiki.d/base/d/viagra-pill.htm [Pingback]
http://sapid-club.com/soap/base/b/weight-loss.htm [Pingback]
http://airport.by/drupal/files/d/paxil.htm [Pingback]
http://tlcwe.com/cerberus-gui/templates_c/d/viagra-pill.htm [Pingback]
http://iseu.by/board/Packages/a/vicodin.htm [Pingback]
http://iseu.by/board/Packages/c/cialis-online.htm [Pingback]
http://parmleyphotography.com/images/b/levitra-cialis-viagra-comparison.htm [Pingback]
http://iseu.by/board/Packages/d/adipex.htm [Pingback]
http://egmsys.com/pmwiki/wiki.d/base/d/phentermine-prescription.htm [Pingback]
http://kathywolfephotography.com/site_images/sec_photos/c/buy-vicodin.htm [Pingback]
http://sapid-club.com/soap/base/a/xenical.htm [Pingback]
http://glamourshades.com/drupal/files/b/cheepphentermine.htm [Pingback]
http://actionhouse.net/shop/d/alternatives-to-viagra.htm [Pingback]
http://sapid-club.com/soap/base/d/viagraonline.htm [Pingback]
http://thebadd.org/mailcenter/users/ericp-thebadd.org/b/pharmacy.htm [Pingback]
http://karenclarkphotography.com/clientgallery/d/atkins-diet.htm [Pingback]
http://scripts.tlcwe.com/kbase/manage/backup/c/buy-soma.htm [Pingback]
http://thebadd.org/mailcenter/users/ericp-thebadd.org/b/paxil.htm [Pingback]
http://tuttlemedia.com/images/main_page/c/unitedhealthcare.htm [Pingback]
http://glamourshades.com/drupal/files/c/carisoprodol-350-mg.htm [Pingback]
http://airport.by/drupal/files/a/generic-viagra.htm [Pingback]
http://airport.by/drupal/files/d/buycialis.htm [Pingback]
http://actionhouse.net/shop/a/best-diet-pills.htm [Pingback]
http://iseu.by/board/Packages/a/valium.htm [Pingback]
http://actionhouse.net/shop/c/levitra-buy.htm [Pingback]
http://thebadd.org/mailcenter/users/ericp-thebadd.org/c/cialis-online.htm [Pingback]
http://glamourshades.com/drupal/files/c/generic-buy-viagra.htm [Pingback]
http://tuttlemedia.com/images/main_page/d/levitracialisviagracomparison.htm [Pingback]
http://ligakvn.de/new/images/b/hydrocodone-online.htm [Pingback]
http://tuttlemedia.com/images/main_page/b/ephedradietpill.htm [Pingback]
http://tuttlemedia.com/images/main_page/b/genericbuyviagra.htm [Pingback]
http://infobear.com/albums/.users/d/viagra-online.htm [Pingback]
http://frodosnotebook.com/maimag/files/c/cheapviagra.htm [Pingback]
http://www.mjspaca.jeunesse-sports.gouv.fr/images/page/d/xenical.htm [Pingback]
http://infobear.com/albums/.users/d/health.htm [Pingback]
http://asgoodasnews.com/punliners/wordplay/images/a/pharmacy.htm [Pingback]
http://asgoodasnews.com/punliners/wordplay/images/a/paxil.htm [Pingback]
http://nhcfae.org/sendstudio/archive/c/diabetes.htm [Pingback]
http://asgoodasnews.com/punliners/wordplay/images/a/doctor.htm [Pingback]
http://madisonjazz.com/manage/snippet-db/a/paxil.htm [Pingback]
http://www.mjspaca.jeunesse-sports.gouv.fr/images/page/c/zoloft.htm [Pingback]
http://abuw.org/chat/chat/localization/thai/b/celebrex.htm [Pingback]
http://abuw.org/chat/chat/localization/thai/a/pharmacy.htm [Pingback]
http://abuw.org/chat/chat/localization/thai/c/vicodin.htm [Pingback]
http://frodosnotebook.com/maimag/files/a/valium.htm [Pingback]
http://www.idweb.fr/altajuris/images/para/b/order-cialis.htm [Pingback]
http://infobear.com/albums/.users/c/doctor.htm [Pingback]
http://frodosnotebook.com/maimag/files/a/levitra.htm [Pingback]
http://asgoodasnews.com/punliners/wordplay/images/d/buyvalium.htm [Pingback]
http://nhcfae.org/sendstudio/archive/a/propecia.htm [Pingback]
http://www.idweb.fr/altajuris/images/para/c/xenical.htm [Pingback]
http://nhcfae.org/sendstudio/archive/d/prozac.htm [Pingback]
http://frodosnotebook.com/maimag/files/c/paxil.htm [Pingback]
http://writemindedblog.com/wp-content/uploads/2006/a/adipex.htm [Pingback]
http://abuw.org/chat/chat/localization/thai/a/buy-phentermine-online.htm [Pingback]
http://frodosnotebook.com/maimag/files/c/order-tramadol2.htm [Pingback]
http://www.idweb.fr/altajuris/images/para/b/buy-hydrocodone.htm [Pingback]
http://abuw.org/chat/chat/localization/thai/d/cheap-meridia.htm [Pingback]
http://asgoodasnews.com/punliners/wordplay/images/d/meridia.htm [Pingback]
http://madisonjazz.com/manage/snippet-db/b/cheap-tramadol.htm [Pingback]
http://nhcfae.org/sendstudio/archive/d/medical-billing.htm [Pingback]
http://nhcfae.org/sendstudio/archive/b/adipex.htm [Pingback]
http://www.mjspaca.jeunesse-sports.gouv.fr/images/page/a/adipex.htm [Pingback]
http://writemindedblog.com/wp-content/uploads/2006/a/buy-phentermine-online.htm [Pingback]
http://www.mjspaca.jeunesse-sports.gouv.fr/images/page/b/health-care.htm [Pingback]
http://taleea.com/alascripts/alachat/data/d/nexium.htm [Pingback]
http://asgoodasnews.com/punliners/wordplay/images/c/vicodin.htm [Pingback]
http://asgoodasnews.com/punliners/wordplay/images/b/nexium.htm [Pingback]
http://asgoodasnews.com/punliners/wordplay/images/c/buy-viagra-online.htm [Pingback]
http://www.idweb.fr/altajuris/images/para/d/ativan.htm [Pingback]
http://taleea.com/alascripts/alachat/data/c/order-tramadol.htm [Pingback]
http://frodosnotebook.com/maimag/files/b/diet-pills.htm [Pingback]
http://madisonjazz.com/manage/snippet-db/a/doctor.htm [Pingback]
http://nhcfae.org/sendstudio/archive/d/celexa.htm [Pingback]
http://madisonjazz.com/manage/snippet-db/a/pharmacy.htm [Pingback]
http://asgoodasnews.com/punliners/wordplay/images/c/cialisonline.htm [Pingback]
http://taleea.com/alascripts/alachat/data/a/order-viagra.htm [Pingback]
http://frodosnotebook.com/maimag/files/c/doctor.htm [Pingback]
http://taleea.com/alascripts/alachat/data/d/viagraonline.htm [Pingback]
http://infobear.com/albums/.users/b/buy-cialis-online.htm [Pingback]
http://madisonjazz.com/manage/snippet-db/d/meridia.htm [Pingback]
http://frodosnotebook.com/maimag/files/a/generic-viagra.htm [Pingback]
http://taleea.com/alascripts/alachat/data/c/cheapviagra.htm [Pingback]
http://asgoodasnews.com/punliners/wordplay/images/b/health.htm [Pingback]
http://infobear.com/albums/.users/b/buy-cialis.htm [Pingback]
http://infobear.com/albums/.users/a/order-viagra.htm [Pingback]
http://taleea.com/alascripts/alachat/data/b/buy-cialis-online.htm [Pingback]
http://asgoodasnews.com/punliners/wordplay/images/b/propecia.htm [Pingback]
http://frodosnotebook.com/maimag/files/d/propecia.htm [Pingback]
http://asgoodasnews.com/punliners/wordplay/images/c/carisoprodolonline.htm [Pingback]
http://madisonjazz.com/manage/snippet-db/d/ambien.htm [Pingback]
http://www.idweb.fr/altajuris/images/para/d/diet.htm [Pingback]
http://www.ucboxingclub.org/cgi-bin/board.pl?read=6083 [Pingback]
http://madisonjazz.com/manage/snippet-db/d/adipex.htm [Pingback]
http://www.mjspaca.jeunesse-sports.gouv.fr/images/page/b/buy-tramadol-online.htm [Pingback]
http://www.bourgestourisme.com/club-hotelier-bourges/images/hotel/c/buy-carisopr... [Pingback]
http://www.ucboxingclub.org/cgi-bin/board.pl?read=6082 [Pingback]
http://thepaintedbody.com/mambo/images/c/order-viagra.htm [Pingback]
http://tzs.net/photos/g2data/b/generic-viagra.htm [Pingback]
http://kinobg.com/temp/d/drug.htm [Pingback]
http://tzs.net/photos/g2data/b/valium.htm [Pingback]
http://thepaintedbody.com/mambo/images/d/medical-billing.htm [Pingback]
http://tinybum.com/stcbb/images/avatars/d/best-diet-pills.htm [Pingback]
http://used-automotives.com/cache/c/hydrocodone-online.htm [Pingback]
http://pspro-magazine.de/wotlab/acp/lib/c/online-pharmacy.htm [Pingback]
http://rynoweb.com/wp-content/cache/c/generic-viagra.htm [Pingback]
http://mactalla.com/inc/a/drug.htm [Pingback]
http://payperclickguru.com/blog/wp-content/c/nexium.htm [Pingback]
http://dragon.go2england.org/inc/b/soma-online.htm [Pingback]
http://chickeninthewoods.org/blog/b/celexa.htm [Pingback]
http://hterry.com/photogallery/include/a/diabetes.htm [Pingback]
http://mani-admin-plugin.com/oldsite/forums/files/c/hydrocodone.htm [Pingback]
http://piopionz.com/gallery2/modules/mapOrig/c/viagra-discount.htm [Pingback]
http://mani-admin-plugin.com/oldsite/forums/files/b/cheap-cialis.htm [Pingback]
http://chickeninthewoods.org/blog/d/orderhydrocodone.htm [Pingback]
http://evolans.com/woot/base/a/ultram.htm [Pingback]
http://frenchseoteam.com/ecrire/inc/b/buy-vicodin.htm [Pingback]
http://dragon.go2england.org/inc/c/buyxanaxonline.htm [Pingback]
http://mobikefed.org/2002/b/nexium.htm [Pingback]
http://mobikefed.org/2002/a/tramadol-online.htm [Pingback]
http://mobikefed.org/2002/c/zoloft.htm [Pingback]
http://mobikefed.org/2002/b/xenical.htm [Pingback]
http://www.tiergeschichten.de/images/a/buy-viagra.htm [Pingback]
http://www.gaslamp.org/backoffice/b/buyviagra.htm [Pingback]
http://www.gaslamp.org/backoffice/c/doctor.htm [Pingback]
http://www.tiergeschichten.de/images/d/phentermine-online.htm [Pingback]
http://mobikefed.org/2002/c/health.htm [Pingback]
http://www.tiergeschichten.de/images/e/bvo.htm [Pingback]
http://www.tiergeschichten.de/images/e/bp.htm [Pingback]
http://www.focuspocus.org/photopages/e/bco.htm [Pingback]
http://www.focuspocus.org/photopages/e/bp.htm [Pingback]
http://shop.trovata.com/debug/inc/4/celebrex.php [Pingback]
http://petroleumafrica.com/admin/about/4/buyphentermine.php [Pingback]
http://petroleumafrica.com/admin/about/4/carisoprodolonline.php [Pingback]
http://www.clairedanes.com/nb/gallery/g2data/inc/4/orderphentermine.php [Pingback]
http://www.ifhhro.org/UserFiles/Flash/1/diet.php [Pingback]
http://www.verbum.nl/lib/cache/4/cheap-viagra.php [Pingback]
http://marchalesbica.org.mx/pruebag/backend/2/tramadol-online.php [Pingback]
http://marchalesbica.org.mx/pruebag/backend/1/pharmacy.php [Pingback]
http://www.7rally.com/photos/inc/2/weightwatchers.php [Pingback]
http://www.ifhhro.org/UserFiles/Flash/3/xenical.php [Pingback]
http://www.roleplayingtips.com/j4/chat/4/doctor.php [Pingback]
http://www.roleplayingtips.com/j4/chat/3/viagraonline.php [Pingback]
http://www.drzwi.pl/files/pages_gallery/mini/inc/2/weight-watchers.php [Pingback]
http://www.drzwi.pl/files/pages_gallery/mini/inc/3/soma-online.php [Pingback]
http://www.bixbylibrary.org/cms/images/misc/1/xenical.php [Pingback]
http://www.drzwi.pl/files/pages_gallery/mini/inc/2/drugrehab.php [Pingback]
http://www.mercantilelibrary.com/email-manager/base/3/order-tramadol.php [Pingback]
http://www.distefanowinery.com/images/cat_icons/2/buy-levitra.php [Pingback]
http://www.distefanowinery.com/images/cat_icons/2/phentermine-diet-pills.php [Pingback]
http://www.distefanowinery.com/images/cat_icons/4/online-pharmacy.php [Pingback]
"Order cialis online." (Cheap cialis online order cialis now with discount.) [Trackback]