PowerShell - Creating Active Directory users from a CSV

• Craig

It looks like I’ve been on a bit of a PowerShell trip this week (ha?) - I needed to quickly create about 50 temporary users so decided to push my old CreateUsers.VBS to one side and have a look at what PowerShell can do for me. After reading a few articles I came across this Blog by a guy called Josh Twist and his post on creating user accounts in PowerShell (which was exactly what I was looking for).

You can find the post here

What I especially liked about this script is that it let you enable the accounts and set their passwords all within the foreach loop (not being much of a coder this excided me as I’d seen many examples that didn’t include this) Anyway, here is the code from josh’s blog:

$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")

I definatly recomend making an effort to visit Josh’s site The Joy Of Code and if possible his twitter page.