'PowerShell to create bulk users

I want to apologize in advance I know this is going to be something I am overlooking but I have spent hours trying to get this to work right. Code is at the bottom.

I am trying to create an automated script to create users based on an email sent to a DL with PowerShell. I am not that versed and have exhausted the web and all the books I have. I have gotten it to work but with some errors.

Errors

  1. In AD under the Account tab under the user logon name: the domain field was left empty
  2. Under Active Directory Administrative Center, and Attributes. For Proxy the SMTP was not set.
Import-Module ActiveDirectory
$Users = Import-Csv -Path "C:\Script\newusers2022.csv"            
foreach ($User in $Users)            
{            
   $Displayname = $User.FirstName + " " + $User.Lastname            
   $UserFirstname = $User.Firstname            
   $UserLastname = $User.Lastname  
   $UserEmployeeID = $User.EmployeeID          
   $OU = $User.OU           
   $SAM = $User.FirstName + "." + $User.Lastname            
   $UPN = $User.Firstname + "." +  $User.Lastname + "@" + $User.Mydomain.com
   $EmailAddress = $User.Firstname + "." +  $User.Lastname + "@" + $User.Mydomain.com            
   $Description = $User.Description            
   $Password = "RandomColor1" + "RandomGenPass" + "!"     
   New-ADUser -Name $Displayname -DisplayName $Displayname -SamAccountName $SAM -UserPrincipalName $UPN -GivenName $UserFirstname -Surname $UserLastname -Emailaddress $Email -EmployeeID $UserEmployeeID -Description $Description -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $true -PasswordNeverExpires $false
   Write-Host $User.SAM "created successfully"
}  


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source