PowerShell Script to Create new users in Active Directory

This scripts create users in active directory by look for the details listed in a CSV file which can be download here, and add a proxy email address, add to AD Groups and place them in a OU called temp OU.

You will need to change a number of values to match you environment:

  • @emaildomain.co.uk
  • OU=TempOU,DC=domain,DC=local
  • Name of the groups you would like them to be added to

If I had more time I would add in variable so you can easily change the value (may be in the future)


$csv_info = import-csv "C:\PSScripts\newusers.csv"
foreach ($line in $csv_info) 
{
    $name = $line.GivenName + " " + $line.Surname
    $samaccountname = $line.GivenName + "." + $Line.surname
    $emailaddress1 = $samaccountname + "@emaildomain.co.uk"
    $emailaddress = $emailaddress1.ToLower()
    $GROUP1 = $line.GROUP1
    $GROUP2 = $line.GROUP2
    write-host $name
    write-host $samaccountname
    write-host $emailaddress
    New-ADUser -GivenName $line.GivenName -Surname $line.Surname -Name $name -DisplayName $Name -UserPrincipalName $emailaddress -SamAccountName $SamAccountName -EmailAddress $emailaddress -Enabled $True -AccountPassword (ConvertTo-SecureString $line.Password -AsPlainText -force) -path 'OU=TempOU,DC=domain,DC=local'
    Set-ADUser $samaccountname -add @{ProxyAddresses="SMTP:$emailaddress"}
 
  If($GROUP1 -eq "YES")
 {
   Add-ADGroupMember -Identity "GROUP1" -Member $samaccountname
   write-host "Added to GROUP1"
 }

 If($GROUP2 -eq "YES")
 {
   Add-ADGroupMember -Identity "GROUP2" -Member $samaccountname
   write-host "Added to GROUP2"
 }
 
}