We help IT Professionals succeed at work.

Powershell script to create AD accounts from spreadsheet - revisited

Ben Hart
Ben Hart asked
Last Modified: 2012-05-09
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 name...so: 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 = "difc.root01.org"

$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
	$newuser.sn = $_.SN
	$newuser.displayName = $_.displayName
	$newuser.userPrincipalName = $_.givenName[0]+$_.givenName[1]+$_.SN + "@" + $domain
    $newuser.company =$_.company
    $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


Open in new window

Watch Question

PowerShell Developer
Top Expert 2010
This one is on us!
(Get your first solution completely free - no credit card required)


That actually didn't work for me.. it complained about the parenthesis.  However changing it to:

$_.givenName + $_.SN

Did net the results I wanted.  You pointed me in the right direction so Thanks.

Not that this isn't useful code, but shouldn't it be in the powershell topic and not the VBS topic?

My apologies..  For some reason this showed up under the articles section on the front page of the VBS topic.  I didn't realize that they apparently now also show solutions there...

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.