Newbie powershell learner here.
I wanted to create a script that will automate the bulk creation of our AD
I have an import csv with list of name,location,OU,etc. (I wanted to remove the OU property on the CSV file and have a script where it will automatically put in the user account on it’s OU by each location)
if (! (Get-ADUser -Filter {SamAccountName -eq $Username})) {
Write-Warning “A user account $Username has already exist in Active Directory.” }
else
#If a user does not exist then create a new user account
#Account will be created in the OU listed in the $OU variable in the CSV file; don’t forget to change the domain name in the"-UserPrincipalName" variable
New-ADUser `
-SamAccountName $Username`
-UserPrincipalName “$Username@corp.test” `
-Name “$Firstname $Lastname”`
-GivenName $Firstname `
-Surname $Lastname`
-Enabled $True `
-ChangePasswordAtLogon $True`
-DisplayName “$Lastname, $Firstname” `
-Department $Department`
-Path $OU `
-Description $description`
-state $State `
-street $Street`
-office $Office `
-EmailAddress $Email`
-city $city `
-PostalCode $Zipcode`
-Country $Country `
-Title $Jobtitle`
-Company $Company ` -AccountPassword (convertto-securestring $password -AsPlainText -Force)
# Create user based on csv column headers
Import-Csv -Path ADUser.csv | ForEach-Object {
If (Get-ADUser -Filter {SamAccountName -eq $_.UserName}){
Write-Warning “$($_.UserName) exist in Active Directory.”} Else {
$adprop = @{
SamAccountName = $_.Username
UserPrincipalName = "$($_.Username)@corp.test"
Name = "$($_.Firstname) $($_.Lastname)”
GivenName = $_.Firstname
Surname = $_.Lastname
Enabled = $True
ChangePasswordAtLogon = $True
DisplayName = “$($_.Lastname),$($_.Firstname)”
Department = $_.Department
Path = $_.OU
Description = $_.description
state = $_.State
street = $_.Street
office = $_.Office
EmailAddress = $_.Email
city = $_.city
PostalCode = $_.Zipcode
Country = $_.Country
Title = $_.Jobtitle
Company = $_.Company
AccountPassword = (ConvertTo-SecureString $_.password -AsPlainText -Force)}}
New-ADUser @adprop
}
How are you generating the username? There is usually name collisions like John Smith, Jane Smith, etc., so it’s a bit more involved that joining parts of a name. If you have that information from HR, you should also have the EmployeeId and should set it in AD. Eventually you will want to reconcile the data to ensure there is a match 1:1 for users, and then your script can update existing users and create new users.