Code in this video_How to Create Bulk User Account in Active Directory with CSV file
📌 OU Structure
📌 CSV Template file
FirstName | LastName | OU | Username | Password | Department | JobTitle | Telephone | StreetAddress | City | ZipCode | State | Country | Company | |
HR | 1 | OU=HR,OU=HG,DC=hg,DC=local | hr1 | Password | [email protected] | HR | ||||||||
Accounting | 1 | OU=Accouting,OU=HG,DC=hg,DC=local | acc1 | Password | [email protected] | Accounting | ||||||||
Purchasing | 1 | OU=Purchasing,OU=HG,DC=hg,DC=local | pur1 | Password | [email protected] | Purchasing | ||||||||
Production | 1 | OU=Production,OU=HG,DC=hg,DC=local | prod1 | Password | [email protected] | Production |
📌 Script
# Import active directory module for running AD cmdlets Import-Module ActiveDirectory # Store the data from MultiUser.csv in the $ADUsers variable $ADUsers = Import-Csv full_path_of_file_MultiUser.csv # Define UPN, replace hg.local = Your-UPN $UPN = "hg.local" # 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 $username = $User.username $password = $User.password $firstname = $User.firstname $lastname = $User.lastname $initials = $User.initials $OU = $User.ou $email = $User.email $streetaddress = $User.streetaddress $city = $User.city $zipcode = $User.zipcode $state = $User.state $country = $User.country $telephone = $User.telephone $jobtitle = $User.jobtitle $company = $User.company $department = $User.department # 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 "A user account with username $username already exists 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 "$username@$UPN" ` -Name "$firstname $lastname" ` -GivenName $firstname ` -Surname $lastname ` -Initials $initials ` -Enabled $True ` -DisplayName "$lastname, $firstname" ` -Path $OU ` -City $city ` -PostalCode $zipcode ` -Country $country ` -Company $company ` -State $state ` -StreetAddress $streetaddress ` -OfficePhone $telephone ` -EmailAddress $email ` -Title $jobtitle ` -Department $department ` -AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -ChangePasswordAtLogon $True # If user is created, show message. Write-Host "The user account $username is created." -ForegroundColor Cyan } } Read-Host -Prompt "Press Enter to exit"