Advertisement
Guest User

Untitled

a guest
Sep 1st, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Imports users from HR csv and which creates an object with properties that match the column headers
  2. $users = Import-Csv -Path 'C:\Path\to\CSV'
  3.  
  4. #Defines the default password for new accounts
  5. $AccountPassword = $(ConvertTo-SecureString -String "New User Default Password" -AsPlainText -Force)
  6.  
  7. #Loops through each row in the csv, creates variable for each property it got from the csv and then creates the user.
  8. foreach ($user in $users){
  9.  
  10.     #These match the column names in the csv
  11.     $NewUserSplat = @{
  12.         SamAccountName = $user.Username
  13.         Name = "$($user."Last Name"), $($user."First Name") $($user.Int)."
  14.         GivenName = $user."First Name"
  15.         Surname = $user."Last Name"
  16.         Initials = $user.Int
  17.         DisplayName = "$($user."Last Name"), $($user."First Name") $($user.Int)."
  18.         UserPrincipalName = "$($user.Username)@company.org"
  19.         Description = "$($user."Position") - $($user."Department")"
  20.         EmployID = $user."ID"
  21.         OU = "ou=OU,dc=company,dc=org"
  22.         Enabled = $true
  23.         CannotChangePassword = $false
  24.         ChangePasswordAtLogon = $true
  25.         AccountPassword = $AccountPassword
  26.     }
  27.  
  28.     #Creates the user in AD
  29.     $NewUser = New-ADUser @NewUserSplat -PassThru
  30.     $NewUserName = Get-ADUser -Identity $NewUser
  31.  
  32.     #Adds the new user we just created to group one
  33.     $GroupOne = 'group one'
  34.     Add-ADGroupMember -Identity $GroupOne -Members $NewUserName
  35.  
  36.     #Adds new user we just created to group two
  37.     $GroupTwo = 'group two'
  38.     Add-ADGroupMember -Identity $GroupTwo -Members $NewUserName
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement