title: How to add multiple users in to multiple groups in a new users script?
body: <p>I have a script which is making active directory users, and it's working great.</p>
  
  <p>Here a thing, i need that these users will add them self after the creation to </p>
  
  <p>some groups.</p>
  
  <p>So i've figured out that thre is a cmdle <code>Add-ADPrincipalGroupMembership</code></p>
  
  <p>but I don't know how to combine this CmdLet into my script ( i'm on PowerShell abit more the a month)</p>
  
  <p>i've tried to use another foreach statement but it didn't worked</p>
  
  <p>Here is the Code:</p>
  
  <pre><code>cls
  #get the csv file
  $filepath = import-csv "C:\users.csv"
  
  #set the variable for the uers
  $newusers = $filepath
  
  #set Passwords for new users 
  $securepassword = ConvertTo-SecureString "blahblah" -AsPlainText -Force
  
  #start the loop 
  foreach ($user in $newusers) {
  
      #get user information
      $firstname = $user.'First Name'.Trim()
      $lastname = $user.'Last Name'.Trim()
      $loginname= $user.SamAccountName
      $UsrPrincipalName = $user.UserPrincipalName
      $jobtitle = $user.'Job Title'
      $Department= $user.Department
      $Description = $user.Description
      $OuPath= $user.Path
      $LoginScript=$user.ScriptPath
      $displayname= $user.DisplayName
  
      #create the users in active directory
      $vars = @{
          Name = "$firstname $lastname"
          GivenName = $firstname
          Surname = $lastname
          UserPrincipalName = $UsrPrincipalName
          SamAccountName = $loginname
          Path = $OuPath
          ScriptPath = $LoginScript
          AccountPassword = $securepassword
          ChangePasswordAtLogon = $false
          Department = $Department
          DisplayName = $displayname
          Description = $Description
          Title = $jobtitle
          Enabled = $true
      }
  
      #Editors comment: Make a hashtable and use splatting when specifying lots of parameters
      $newcreatedusers = New-ADUser @vars -PassThru
  
      #starting a loop for adding the users to the groups
      Write-Host "`n"
      Write-Host "The account for $firstname $lastname created in $OuPath successfully"
  
  }
  
  
  $filepath = $Adgroups
  
  foreach ($group in $Adgroups){
  
      $adgroup = $group.Groups.splite(',')
  
      Add-ADPrincipalGroupMembership -Identity $group.Groups -members $SamAccountName
  
  }
  </code></pre>
  
  <p>the CSV file:
  <a href="https://i.stack.imgur.com/9Sf8u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9Sf8u.png" alt="CSV Sample"></a></p>