Powershell – Create AD User script

This is a script which I use to create Users in bulk

#Import active directory module for running AD cmdlets
Import-Module ActiveDirectory

#Get DNS root of the domain
$dnsroot = '@' + (Get-ADDomain).dnsroot
  
#Store the data from ADUsers.csv in the $ADUsers variable csv is not comma separated -Delimiter ";"
$ADUsers = Import-csv C:\Temp\Indienst_20200610.csv -Delimiter ";"

#Loop through each row containing user details in the CSV file 
foreach ($User in $ADUsers)
{
	#Read user data from each field in each row and assign the data to a variable as below
#Soort Mutatie;Aanhef;Achternaam;Meisjesnaam;Voornaam;Roepnaam;Voorletters;Geboortedatum;Personeelsnummer;Aard Indiesttreding;Tijdelijke Einddatum;Dienstverband Percentage;Bedrijf;Parkeerkaart;Functienaam Nieuw;Wachtdienstpas;Afdeling Nieuw;Naam Leidinggevende;Standplaats Nieuw;Kamernummer Nieuw;Eerste Werkdag;Contract Begin;Naam Opvang;Telefoonnummer Nieuw;Inlognaam;Mailadres;Opmerkingen

		
   $Firstname = $User.Roepnaam
   $Firstletter = $Firstname.substring(0,1)
   $Initials = $User.Voorletters
   $Maydenname = $User.Meisjesnaam
   $Lastname = $User.Achternaam
   $Aanhef =$User.Aanhef
   $Bday = $User.Geboortedatum
   $Username = $Lastname.substring(0,4)+$Firstname.substring(0,1)+$Bday
#   $Password = $User.password
   $OU = "OU=Users-Create,DC=cloudshark,DC=nl"
#   $email = "$Firstletter.$Lastname$dnsroot"
#   $streetaddress = $User.streetaddress
#   $city = $User.city
#   $zipcode = $User.zipcode
#   $state = $User.state
#   $country = $User.country
#   $telephone = $User.telephone
   $jobtitle = $User.FunctienaamNieuw
   $company = $User.Bedrijf
   $department = $User.AfdelingNieuw
   $Password = "Welkom123456"
   $employeeID = $User.Personeelsnummer
   $office = $User.StandplaatsNieuw + " - " + $User.KamernummerNieuw
#   $enddate = $User.TijdelijkeEinddatum -format u



if ($Aanhef -eq 'Dhr.') {$emaillastname = $Lastname} else {$emaillastname = $Maydenname}

#   echo $emaillastname

#Check to see if the user already exists in AD
	if (Get-ADUser -F {SamAccountName -eq $Username})
	{
		#If user does exist, give a warning
		 Write-Warning "Warning : Account with username $Username already exist in Active Directory."
	}
	else
	{

        

#User does not exist then proceed to create the new user account
#Account will be created in the OU provided by the $OU variable read from the CSV file


		New-ADUser `
            -SamAccountName $Username `
            -UserPrincipalName "$Firstletter.$Lastname$dnsroot" `
            -Name "$Firstname $Lastname" `
            -GivenName $Firstname `
            -Surname $Lastname `
            -initials $Initials `
            -Enabled $True `
            -DisplayName "$Lastname, $Firstname" `
            -Path $OU `
            -City $city `
            -Company $company `
            -State $state `
            -PostalCode $zipcode `
            -StreetAddress $streetaddress `
            -Country NL `
            -OfficePhone $telephone `
            -mobile $telephone `
            -Title $jobtitle `
            -Department $department `
            -employeeID $employeeID `
            -office $office `
            -EmailAddress "$Firstletter.$emaillastname$dnsroot" `
            -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True
            
	}
 
 
}



Leave a Reply

Your email address will not be published. Required fields are marked *