Create user in bulk (Perl)

This code can be found in Chapter 6 of Active Directory Cookbook, 2nd edition

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# This Perl code creates a large number of users with incremented user names
# e.g. User1, User2, User3, ....

# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
#      "Active Directory Cookbook" by Robbie Allen
# ISBN: 0-596-00466-4
# ---------------------------------------------------------------

# ------ SCRIPT CONFIGURATION ------
my $intNumUsers = 1000;         # Number of users to create
my $strParentDN = "<ParentDN>"; # e.g. ou=bulk,dc=emea,dc=rallencorp,dc=com
# ------ END CONFIGURATION ---------
use Win32::OLE;
$Win32::OLE::Warn = 3;
# Taken from $ADS_USER_FLAG_ENUM
my $ADS_UF_NORMAL_ACCOUNT = 512;
my $objParent = Win32::OLE->GetObject("LDAP://" . $strParentDN);
foreach my $i ( 1 .. $intNumUsers) {
   $strUser = "User" . $i;
   my $objUser = $objParent->Create("user", "cn=" . $strUser);
   $objUser->Put("sAMAccountName", $strUser);

# CORRECTION: If you don't set userAccountControl, then by default
#             the value of 514 (normal account + disabled) will be set for it.
#             In this instance by setting it to 512, the account will not
#             be disabled, and if you have password complexity enabled in
#             your forest, the script will fail because a password was not
#             set prior to the account being enabled.  The solution is to 
#             not set userAccountControl here.
#   $objUser->Put("userAccountControl", $ADS_UF_NORMAL_ACCOUNT);

   $objUser->SetInfo;
   $objUser->SetPassword($strUser);
   $objUser->{AccountDisabled} = FALSE;
   $objUser->SetInfo;
   print "Created $strUser\n";
}
print "\nCreated $intNumUsers users\n";

This code has been viewed 2074 times.

New from the creators of TechTasks.com: StatSheet.com