Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #Import the PowerShell module containing AD cmdlets
  2. Import-Module ActiveDirectory
  3.  
  4. write-host "Start Process"
  5. write-host "-------------------------------------"
  6.  
  7. try
  8. {
  9. #Read the CSV file
  10. $csvPath = "C:\0010798M.csv"
  11. $csvData = import-csv $csvPath
  12.  
  13. write-host "Reading the CSV file......"
  14. #Loop through all items in the CSV items
  15.  
  16. ForEach ($user In $csvData)
  17. {
  18. $saMAccountName = $user.sAMAccountName
  19.  
  20. #Check if the User exists
  21. $ADuser = Get-ADUser -LDAPFilter "(sAMAccountName=$saMAccountName)"
  22.  
  23. If ($ADuser -eq $Null)
  24. {
  25. #Create user using New-ADUser cmdlet
  26. $userPrincipalName = $user.sAMAccountName + "@adatum.com"
  27.  
  28. New-ADUser -Name $user.displayName `
  29. -SamAccountName $sAMAccountName `
  30. -UserPrincipalName $userPrincipalName `
  31. -GivenName $user.givenname `
  32. -Surname $user.sn `
  33. -DisplayName $user.displayName `
  34. -AccountPassword (ConvertTo-SecureString "Pa`$`$w0rd" -AsPlainText -Force) `
  35. -PasswordNeverExpires $true `
  36. -ChangePasswordAtLogon $false `
  37. -Enabled $true
  38.  
  39. write-host "- " $user.sAMAccountName "| Account Created" -ForegroundColor green
  40.  
  41. }
  42. else
  43. {
  44. write-host "- " $user.sAMAccountName "|Account Exists" -ForegroundColor yellow
  45. }
  46. }
  47. }
  48.  
  49. catch
  50. {
  51. write-host "Error: " $($_.CategoryInfo) -ForegroundColor red
  52. write-host "Message: " $($_.Exception.Message) -ForegroundColor red
  53.  
  54. }
  55.  
  56. write-host "-----------------------------------------------------------------"
  57. write-host "End Process"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement