locked
Bulk import users into AD from CSV RRS feed

  • Question

  • Good morning everyone,

    Long time lurker, first time poster.  I am trying to do a few things with PS and I am completely lost.  The CSV contains a user's first name, middle initial, and last name.  The AD user will be created from the first and middle initial with the last name (John Q. Public becomes jqpublic).  The users will be added to two specific groups and one specified OU.

    I'll be honest:  I have never worked with PS and am having a horrible time figuring out how to do this.  I've searched the forums and found a few things that may or may not be relevant, however each OP seems to have at least some background with PS; all posts seem to dive right in without great explanation.  I'm not looking to particularly have my hand held, but if someone could point me in the right direction, I would greatly appreciate the help.

    Thank you to anyone who can take the time to help me out.  Take care!

    -Travis

    Thursday, March 3, 2011 2:38 PM

Answers

  • OK the layout has gone a bit pete tong above but hopefully it looks better below:

    $NewUsers = import-csv c:\folder\filename.csv
    $group1 = Get-QADGroup "groupname1"
    $group2 = Get-QADGroup "groupname2"
    
    #Start looping through the users in the array and create an account for each one, then join that account to the 2 groups
    
    foreach($User in $NewUsers)
    {
    
    #Build your username from your requirements above (first letter, initial, lastname), substring lets us select character 0 and we only want 1 character 
    
    $LogonName = $User.firstname.Substring(0,1) + $User.initial + $User.lastname
    
    #create the actual user account in the correct OU
    
    $CreatedUser = new-qadUser -ParentContainer 'OU=companyOU,DC=company,DC=com' -FirstName $User.firstname -LastName $User.lastname -Initials $User.initial -SamAccountName $LogonName 
    
    #join the user to both groups
    
    Add-QADGroupMember $group1 $CreatedUser
    Add-QADGroupMember $group2 $CreatedUser
    } 
    
    • Marked as answer by Dale Qiao Tuesday, March 8, 2011 1:20 AM
    Thursday, March 3, 2011 4:00 PM

All replies

  • Firstly I always recommend to people to try out the Quest AD cmdlets.

    They are free and take some of the pain out of managing AD from powershell.

    http://www.quest.com/powershell/activeroles-server.aspx

    Lets say your CSV fields are firstname, initial, lastname

    Breaking down the script i would do this:

    #Obtain your user details from your CSV file and store them in an array, then obtain the group properties for the 2 groups to join users to

    $NewUsers

     

     

    = import-csv c:\folder\filename.csv
    $group1 = Get-QADGroup "groupname1"
    $group2 = Get-QADGroup "groupname2"

    #Start looping through the users in the array and create an account for each one, then join that account to the 2 groups

    foreach

     

     

    ($User in $NewUsers)
    {

    #Build your username from your requirements above (first letter, initial, lastname), substring lets us select character 0 and we only want 1 character

    $LogonName

     

     

    = $User.firstname.Substring(0,1) + $User.initial + $User.lastname

    #create the actual user account in the correct OU

    $CreatedUser = new-qadUser -ParentContainer 'OU=companyOU,DC=company,DC=com' -FirstName $User.firstname -LastName $User.lastname -Initials $User.initial -SamAccountName $LogonName

     

     

     

     

     

     

    #join the user to both groups

    Add-QADGroupMember

     

     

     

    $group1 $CreatedUser
    Add-QADGroupMember $group2 $CreatedUser
    }

    As always use scripts at your own risk, test in a test environment etc...etc....

     

     

    Thursday, March 3, 2011 3:56 PM
  • OK the layout has gone a bit pete tong above but hopefully it looks better below:

    $NewUsers = import-csv c:\folder\filename.csv
    $group1 = Get-QADGroup "groupname1"
    $group2 = Get-QADGroup "groupname2"
    
    #Start looping through the users in the array and create an account for each one, then join that account to the 2 groups
    
    foreach($User in $NewUsers)
    {
    
    #Build your username from your requirements above (first letter, initial, lastname), substring lets us select character 0 and we only want 1 character 
    
    $LogonName = $User.firstname.Substring(0,1) + $User.initial + $User.lastname
    
    #create the actual user account in the correct OU
    
    $CreatedUser = new-qadUser -ParentContainer 'OU=companyOU,DC=company,DC=com' -FirstName $User.firstname -LastName $User.lastname -Initials $User.initial -SamAccountName $LogonName 
    
    #join the user to both groups
    
    Add-QADGroupMember $group1 $CreatedUser
    Add-QADGroupMember $group2 $CreatedUser
    } 
    
    • Marked as answer by Dale Qiao Tuesday, March 8, 2011 1:20 AM
    Thursday, March 3, 2011 4:00 PM
  • Hi Gary,

    I know this is way too late, but I wanted to thank you for all your help.  While waiting for a response, I stumbled upon a script that basically did what I wanted, however your $CreatedUser variable really pushed it over the edge for me and helped me see exactly what I needed to do.  Thanks again for all your help and time!

    -Travis

    Monday, March 21, 2011 6:04 PM
  • Travis,

    Can you share where you stumbled upon that script. I would be interested in looking at it as well.

     

    Thanks

    Tuesday, July 12, 2011 6:19 PM