Creating AD user accounts in PowerShell
I spent last week in the Microsoft UK Performance and Scalability labs with an
ADC customer. We had to load test their web application which used windows auth but had a custom roleprovider. This meant that we needed to script out a bunch of AD accounts for our load tests to use. Needless to say, I turned to powershell :)
We created a list of users names and dropped them into a CSV file with just one column: UserName.
$users = import-csv "C:\usersToBeCreated.csv"
$container = [ADSI] "LDAP://cn=Users,dc=YourDomain,dc=local"
$users | foreach {
$UserName = $_.UserName
$newUser = $container.Create("User", "cn=" + $UserName)
$newUser.Put("sAMAccountName", $UserName)
$newUser.SetInfo()
$newUser.psbase.InvokeSet('AccountDisabled', $false)
$newUser.SetInfo()
$newUser.SetPassword("P@55w0rd")
}
This was all easy enough but once again, this script isn't perfect. The accounts the script created were disabled but I couldn't, for the life of me, get PowerShell to enable the scripts. It seems I should just have to say $newUser.AccountDisabled = $false? Anyway, we didn't have much time to waste so I simply popped into the AD GUI and selected all the accounts (all 6000 of them) and selected Enable. Done.
UPDATE - Thanks to BomBom's comment the script now enables the accounts propertly too! Thanks BomBom!
Note - this script was run on the domain controller itself.

Post By
Josh Twist
01:43
30 Jun 2008
» Next Post:
Updated Silverlight Uploader for SL2 Beta 2
« Previous Post:
How to remove a file attribute in PowerShell
Comments:
Posted by
Scott Dukes
@
30 Jun 2008
02:18
Or you could just use Quest's (free) AD cmdlets - which rock :-)
http://www.quest.com/powershell/activeroles-server.aspx
Posted by
BomBom
@
08 Jul 2008
03:29
Do it like this:
$newUser.psbase.InvokeSet('AccountDisabled', $false)
$newUser.SetInfo()
Has to be done in the right order (create user, SetInfo, enable account, SetInfo)
Posted by
josh
@
27 Jan 2009
00:41
Thanks BomBom - post updated!