Automatically add user and ou to Active Directory at the same time

Hello good time

I had a question about creating a user and an organizational unit that I was very busy with and in the end I could not find the answer and I asked you for help. My question is that I have an Excel that contains the following specifications:

id, fname, lname, email, employeeid, postcode, country, mobile, displayname, username, address, department, job

I want to write a script in PowerShell that automatically creates an organizational unit in the Active Directory relative to the desired department with these specifications and automatically adds any person who belongs to that department in that organizational unit. In general, I want to give all the Excel I have in PowerShell and add both the organizational unit and the user based on this Excel, that is, I will move them softly after adding the user in the organizational units, do it automatically Can you help me in this regard? I searched your entire site and abroad, but I did not come to any conclusion Thank you

While you can read xls files, it would be better to have the output be a csv as it’s directly imported into Powershell with minimal code.

Import-CSV - Import the data
Group-Object - Try grouping on property department
Get-ADOrganizationalUnit - Check to see if the OU exists
New-AdOrganizationUnit - Create new OU
Move-ADObject - Move the user into the OU

Think of it step by step. Get the data in. Write a for loop to see if the department OU exists and create it if not. Add users to department OUs. Make sure when running AD command that you specify the -Server with the DC so that you are checking and creating objects on the same server and can validate by connection the console to that server. Try to get some code built and check back here with questions.