Bulk Active Directory user onboarding from a CSV file, using PowerShell.
Note: This script is for lab purposes only. It is not intended for production use.
Import-Users.ps1 automates onboarding of bulk users into Active Directory. It reads user records from a CSV file and creates each account in the department OU that sits under a base Organizational Unit you specify. Before creating a user, the script checks that the department OU exists and that the account is not already present, and skips the record if either check fails. New accounts are created enabled, with the "change password at next logon" flag set.
| Item | Details |
|---|---|
| PowerShell version | Tested On: Windows PowerShell 5.1 |
| Operating system | Windows machine with the Active Directory PowerShell module |
| Modules | ActiveDirectory (the script uses Get-ADUser, Get-ADOrganizationalUnit and New-ADUser) |
| Permissions | An account allowed to create user objects in the target OUs |
| Environment | Department OUs must already exist directly under the base OU |
git clone https://github.com/subhadeep404/Import-Users.git
cd Import-UsersAlternatively, download Import-Users.ps1 and sample-users.csv from the repository.
| Flag | Type | Required | Default | Description |
|---|---|---|---|---|
-Csv |
Not declared (path to a CSV file) | Yes | None | Path to the CSV file containing the user records. |
-BasePath |
String |
Yes | None | Distinguished name of the base OU. A department OU must exist under it for each department in the CSV. Example: OU=Users,OU=Contoso,DC=contoso,DC=local |
The script has no switches, parameter sets or ValidateSet values.
The first row must contain these column headers:
| Column | Description |
|---|---|
First Name |
Given name |
Last Name |
Surname |
Display Name |
Display name; also used as the account's Name (CN) |
Department |
Name of the department OU under -BasePath where the account is created |
Title |
Job title |
SAM |
SamAccountName |
UPN |
UserPrincipalName |
Password |
Initial password, read as plain text |
Example (use dummy data only):
First Name,Last Name,Display Name,Department,Title,SAM,UPN,Password
Jane,Doe,Jane Doe,HR,HR Manager,jane.doe,jane.doe@contoso.local,<temp-password>Imports the users from the sample CSV into the department OUs under the base OU.
.\Import-Users.ps1 -Csv .\sample-users.csv -BasePath "OU=Users,OU=Contoso,DC=contoso,DC=local"Expected result: each record prints [+] Importing User: <Display Name> and the account is created in OU=<Department> under the base OU.
.\Import-Users.ps1 -Csv C:\Temp\users.csv -BasePath "OU=Users,OU=Contoso,DC=contoso,DC=local"Expected result: the accounts are created enabled, with a password change required at first logon.
.\Import-Users.ps1 -Csv C:\Temp\users.csv -BasePath "OU=Users,OU=Contoso,DC=contoso,DC=local"Expected result: users already found under the base OU are reported as [-] <name> already exists in <Department> and skipped.
.\Import-Users.ps1 -Csv C:\Temp\users.csv -BasePath "OU=Users,OU=Contoso,DC=contoso,DC=local"Expected result: if a record's department has no matching OU, the script prints [!] <Department> does not exist. and skips that record. Be aware of the password exposure described under Known Bugs.