title: Powershell Script Create Bulk Users and assign them to multiple groups
body: <p>I've been working on a script that imports users in the Active Directory assigned to multiple groups through PowerShell. The script is working fine, but I'd like to know if I can improve the script and maybe you guys have any suggestions or improvements on the script? </p>
  
  <p><strong>Powershell Script: Import_BulkUsers.ps1</strong></p>
  
  <pre><code>$CSVFile = "Gebruikers.csv"
  
  $users = Import-Csv $CSVFile -delimiter ";"
  
  $users | ForEach-Object {
      # Domain data
      $OU = $_.Afdeling
      $DomainName = "LAN.CONTOSO.COM"
  
      # User data
      $voornaam = $_.Voornaam
      $tussenvoegsel = $_.Tussenvoegsel
      $achternaam = $_.Achternaam
      $password = "Password1"
      $username = $voornaam.Substring(0,3) + $achternaam.Substring(0,2)
  
      if($tussenvoegsel -eq "") { 
          $Displayname= $voornaam + " " + $achternaam 
      } else {
          $Displayname= $voornaam + " " + $tussenvoegsel + " " + $achternaam
      }
  
      # Userprofile &amp; Homefolder
      $homefolderdrive = "Z:"
      $homefolder = "\\"+$_.SRV+"\"+$_.HF+"$\"+$username
      $profilefolder = "\\"+$_.SRV+"\"+$_.UP+"$\"+$username
  
      # Secure password
      $SecurePass = ConvertTo-SecureString $password -AsPlainText -Force
  
      $UserPrincipalName = $username+"@"+$DomainName
  
      $userExists = (Get-ADUser -Filter { SamAccountName -eq $username }) 
  
          if($userExists -eq $null) {
              # New AD user 
              New-ADUser -Name $Displayname -GivenName $voornaam -Surname $achternaam -DisplayName $Displayname -SamAccountName $username -UserPrincipalName $UserPrincipalName -HomeDrive $homefolderdrive -HomeDirectory $homefolder -ProfilePath $profilefolder -Path $OU -AccountPassword $SecurePass -PasswordNeverExpires $true -Enabled $True
  
              # Add users to Global Groups
              $Groups = ($_.GroupName).split(",")
              foreach ($Group in $Groups) {
                      Add-ADPrincipalGroupMembership -Identity $username -MemberOf $Group
              }
  
              # Create homefolder
              New-Item $homefolder -Itemtype Directory | Out-Null
  
              Write-Host "You've created a new user:" $Displayname -fore "Green"
              Write-Host "`n"
          } else {
              # Display error on screen
              Write-Host "Not imported user:" -Fore "Yellow"
              Write-Host "User '$Displayname' already exists" -Fore "Red"
              Write-Host "`n"
  
              # Function write error to file
              function writetoPath() {
              $dateNow = Get-Date -Format "dd-MM-yyyy @ hh:mm:ss"
              $existingUser = "User '$Displayname' already exists"
              "$dateNow   //   $existingUser" &gt;&gt; C:\LogFiles\PowerShell\Import_Gebruikers.txt
              }
  
              # Create Path if doesn't exists
              $logfilesPath = "C:\LogFiles\PowerShell"
              if (!(Test-Path $logfilesPath)) {
                  New-Item -ItemType Directory -Force -Path $logfilesPath
                  writetoPath
              } else {
                  writetoPath
              }
              }
  }
  </code></pre>
  
  <p><strong>CSV file: Gebruikers.csv</strong>
  <a href="https://i.stack.imgur.com/iO3G7.png" rel="nofollow noreferrer">Gebruikers.csv</a></p>