Welcome › Forums › General PowerShell Q&A › Bulk creation of AD user in multiple OU
- This topic has 2 replies, 3 voices, and was last updated 4 weeks ago by
Participant.
-
AuthorPosts
-
-
December 19, 2020 at 7:29 pm #281147
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 “[email protected]”
-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)
-
December 20, 2020 at 12:50 pm #281284PowerShell12345678910111213141516171819202122232425262728# Create user based on csv column headersImport-Csv -Path ADUser.csv | ForEach-Object {If (Get-ADUser -Filter {SamAccountName -eq $_.UserName}){Write-Warning “$($_.UserName) exist in Active Directory.”} Else {$adprop = @{SamAccountName = $_.UsernameUserPrincipalName = "$($_.Username)@corp.test"Name = "$($_.Firstname) $($_.Lastname)”GivenName = $_.FirstnameSurname = $_.LastnameEnabled = $TrueChangePasswordAtLogon = $TrueDisplayName = “$($_.Lastname),$($_.Firstname)”Department = $_.DepartmentPath = $_.OUDescription = $_.descriptionstate = $_.Statestreet = $_.Streetoffice = $_.OfficeEmailAddress = $_.Emailcity = $_.cityPostalCode = $_.ZipcodeCountry = $_.CountryTitle = $_.JobtitleCompany = $_.CompanyAccountPassword = (ConvertTo-SecureString $_.password -AsPlainText -Force)}}New-ADUser @adprop}
-
December 21, 2020 at 9:04 am #281573
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.
-
-
AuthorPosts
- You must be logged in to reply to this topic.