PowerShell Automation: Creating Active Directory User Accounts

Throughout many of our managed IT projects, we frequently receive requests to set up multiple user accounts at once within a standard Windows Active Directory environment. In fact, there was one project where we needed to create nearly 100 user accounts in a single batch. Instead of manually adding each account through the Active Directory Users and Computers Console—which can be both time-consuming and labour-intensive—let’s look at how we can streamline and automate this process using PowerShell scripting.
Cmdlets
To automate the creation of Active Directory user accounts, we utilize two essential PowerShell cmdlets: Import-Csv and New-ADUser.
The Import-Csv cmdlet reads data from a CSV file and converts each row into a PowerShell object. This enables easy access to user information, such as names and email addresses, which can then be used in scripts for tasks like account creation.

The New-ADUser cmdlet is used to create new user accounts in Active Directory. It allows administrators to specify various attributes for each user, such as their name, username, and organizational unit placement, facilitating efficient and consistent account provisioning.

Assembling the Script
Now that we have gathered the necessary tools (or cmdlets in this case), let's begin crafting our user account creation script.
- We read user account data from a CSV file, which contains details like usernames, full names, and other relevant information by invoking Import-Csv.
- We iterate over each row in the CSV to retrieve the attributes for each user.
- We use these attributes to add the user to the Active Directory domain by invoking New-ADUser.
This process repeats for every user listed in the CSV file, ensuring efficient and consistent account creation across all users. After assembling the steps together, we get the following script:
$companyDomain = "company.com" # Replace company.com with your Active Directory domain name
$newUsersFilePath = "NewUsers.csv"
Import-Csv $newUsersFilePath | ForEach-Object {
New-ADUser `
-Name $_.FullName `
-GivenName $_.FirstName `
-Surname $_.LastName `
-Department $_.Department `
-Title $_.JobTitle `
-SamAccountName $_.Username `
-UserPrincipalName ("$($_.Username)@$companyDomain") `
-AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -Force) `
-Enabled $true `
-ChangePasswordAtLogon $true
}
Running the Script
To run the script, we will need to perform several steps in series.
- Copy the PowerShell code and save it as Create-Users.ps1 on a system with the necessary privileges to add new Active Directory user accounts.
- Modify the companyDomain variable by replacing it with the domain name of your Active Directory domain.
- Create a CSV file named NewUsers.csv with the following headers:
- FullName
- FirstName
- LastName
- Department
- JobTitle
- Username
- Password
- Add the users' details to the corresponding columns in the CSV file.
- Run Create-Users.ps1.
Summary
In conclusion, automating the creation of Active Directory user accounts using PowerShell and CSV files significantly enhances efficiency and consistency in IT operations. By leveraging cmdlets like Import-Csv and New-ADUser, administrators can streamline the onboarding process, reduce manual errors, and save valuable time. If you need assistance with automating your IT tasks, please fill in the "Contact Us" form on our main site, and our team of professionals will get back to you soon!