Advertisement
munsking

import AD user from CSV

Dec 12th, 2011
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###########################################
  2. #
  3. # adds users to AD from CSV file using the
  4. #   semicolon as a delimiter
  5. #
  6. # the variables $myOU, $myDC1 and $myDC2
  7. #   are for the LDAP path, feel free to
  8. #   modify these
  9. #
  10. ###########################################
  11.  
  12. ### DECLARING VARIABLES ###
  13.  
  14. $myOU = "villach"
  15. $myDC1 = "kurs"
  16. $myDC2 = "local"
  17. #LDAP path to the OU where i want the new users
  18. $objOU = [ADSI]"LDAP://OU=$myOU,DC=$myDC1,DC=$myDC2"
  19. #path to the csv file
  20. $csvurl = "C:\test\importme.csv"
  21. #throwing the csv into a var
  22. $users = Import-Csv $csvurl -delimiter ";"
  23.  
  24. ### THE ACTUAL SCRIPT ###
  25.  
  26. #for each line in the csv do {}
  27. $users | foreach {
  28.     #merging the first and last name to make a username
  29.     $Username = $_.firstname + $_.lastname
  30.     #creating the new $_.class(user or computer or w/e) object in this OU
  31.     $newuser = $objOU.Create($_.class, "cn=" + $_.commonname)
  32.     #giving the new user a sAMAccountName
  33.     $newuser.put("sAMAccountName", $_.commonname)
  34.     #writing the info to the AD
  35.     $newuser.setinfo()
  36.     #enabling account
  37.     $newuser.psbase.invokeset('accountdisabled', $false)
  38.     $newuser.setinfo()
  39.     #setting a password
  40.     $newuser.setpassword("wifi1234!")
  41.     $newuser.setinfo()
  42.     #confirm the creation to the user
  43.     Write-Host created $username
  44. }
  45.  
  46.  
  47. ###########################################
  48. #
  49. # used CSV file:
  50. #
  51. # class;commonname;password;firstname;lastname
  52. # user;m.mouse;wifi1234!;mickey;mouse
  53. # user;d.duck;wifi1234!;donald;duck
  54. # user;g.goof;herpderp;goofy;goof
  55. # user;h.duck;donaldsucks;hewey;duck
  56. # user;f.duck;givecandy;fred;duck
  57. #
  58. ###########################################
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement