troubleshooting Question

Powershell script to create AD accounts from spreadsheet - revisited

Avatar of Ben Hart
Ben HartFlag for United States of America asked on
PowershellVB Script
4 Comments1 Solution586 ViewsLast Modified:
Ok I posted a question a while back on this.. a smart guy helped me out very quickly with a section of code.  I've ran into a problem with that we've just noticed.  All of the user accounts created with that put the username in a format such as: John Smith - JoSmith.  So first two letters of the first name, then last name.  Which is not how we do it here.. it's the standard First initial, last Jsmith.  Here is the code.. I'm hoping someone can point out a simple typo to me..
## define constants
$domainstr = ",dc=difc,dc=root01,dc=org"
$domainnb = "difc"             ## domain netbios name
$domain = ""

$ADs_UF_NORMAL_ACCOUNT = 512   ## enables account and sets password required

## get default passsword - encrypted so not stored in script
#  $defaultPassword = Read-Host "Please enter default Password" -asSecureString

## get the list of users from the CSV file
## if need other user properties can add to CSV
## could speed processing by sortng user list by OU but need code 
## to handle change of OU.  This is simpler as an example

Import-csv c:\users\bhart.difc\desktop\importusers.csv | foreach {

## create user name
#	$strusr = $_.Last + " " + $_.First
    $strusr = $_.SN + " " + $_.givenName
#    $last = $_.SN 
#    $first = $_.givenName
 #   $strusr = $_.last + " " + $_.first
 	$ldapstr = "LDAP://OU=" + $_.OU + $domainstr

	$target = [ADSI] $ldapstr
	$newuser = $target.create("user", "cn=" + $strusr)
        $userid = $_.givenName[0]+$_.givenName[1]+$_.SN
	if ($userid.length -gt 20){$userid = $userid.substring(0,20)}
# echo $userid;exit;      	
	$newuser.samaccountname = $userid.ToString()
	$newuser.givenName =  $_.givenName
	$ = $_.SN
	$newuser.displayName = $_.displayName
	$newuser.userPrincipalName = $_.givenName[0]+$_.givenName[1]+$_.SN + "@" + $domain
    $ =$
    $newuser.mail = $_.mail
    $newuser.division = $_.division
    $newuser.employeeType = $_.empType
    $newuser.employeeID = $_.empID
    $newuser.telephoneNumber = $_.telephoneNumber
    $newuser.description = $_.description

#	$newuser.SetPassword($defaultPassword.ToString())
## normal user that requires password & is enabled
	$newuser.userAccountControl = $ADs_UF_NORMAL_ACCOUNT

# set User must change password at next logon flag
#	$newuser.pwdLastSet = 0
#	$newuser.SetInfo()

## now set the country
#	$newuser.c = $_.Country
#	$newuser.SetInfo()

	Write-Host "Created Account for: "  $newuser.Displayname


Chris Dent
PowerShell Developer
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros