Skip to main content

There are times when you want to bulk import users into Active Directory. You already did an AD users export to CSV file. But, now you like to use that CSV file to import the users back into AD. The fastest and most excellent approach is to use PowerShell. In this article, you will learn how to import AD users from CSV with PowerShell.

Import AD users PowerShell script

The  Import-ADUsers.ps1 PowerShell script will go through the CSV file and create AD users with the below information per user:

  1. Full name
  2. First name
  3. Last name
  4. Display name
  5. User logon name
  6. User principal name
  7. Street
  8. City
  9. State
  10. Postal Code
  11. Country
  12. Job Title
  13. Department
  14. Company
  15. Manager
  16. OU
  17. Description
  18. Office
  19. Telephone number
  20. Email
  21. Mobile
  22. Notes
  23. Account status

Note: The account will have the password set as P@ssw0rd1234. Change this to a different password in the PowerShell script.

How to import users into Active Directory from CSV file

Let’s go through the steps and bulk import Active Directory users from CSV file with PowerShell.

Step 1: Create CSV file with users

Without a CSV file, you can’t use the script and import the users into AD.

  • If you already have an Active Directory, you can run the script in the article  Export AD users to CSV with PowerShell . After that, use that CSV file.
  • Suppose you don’t have an Active Directory to export the AD users to CSV file and only want a CSV file example so you can edit and use it; download the CSV file  ImportADUsers.csv.
Import AD users from CSV with PowerShell CSV file

Step 2: Prepare import AD users PowerShell script

Download and place Import-ADUsers.ps1 PowerShell script on the Domain Controller C:\scripts folder. If you don’t have a scripts folder, create one.

Ensure the file is unblocked to prevent errors when running the script.

Another option is to copy and paste the below code into Notepad. Give it the name Import-ADUsers.ps1 and place it in the C:\scripts folder.

<#
    .SYNOPSIS
    Import-ADUsers.ps1

    .DESCRIPTION
    Import Active Directory users from CSV file.

    .LINK
    traseroute.net/import-ad-users-from-csv-powershell

    .NOTES
    Written by: traseroute
    Website:    traseroute.net
    LinkedIn:   linkedin.com/in/traseroute

    .CHANGELOG
    V2.00, 02/11/2024 - Refactored script
#>

# Define the CSV file location and import the data
$Csvfile = "C:\temp\ImportADUsers.csv"
$Users = Import-Csv $Csvfile

# The password for the new user
$Password = "P@ssw0rd1234"

# Import the Active Directory module
Import-Module ActiveDirectory

# Loop through each user
foreach ($User in $Users) {
    try {
        # Retrieve the Manager distinguished name
        $managerDN = if ($User.'Manager') {
            Get-ADUser -Filter "DisplayName -eq '$($User.'Manager')'" -Properties DisplayName |
            Select-Object -ExpandProperty DistinguishedName
        }

        # Define the parameters using a hashtable
        $NewUserParams = @{
            Name                  = "$($User.'First name') $($User.'Last name')"
            GivenName             = $User.'First name'
            Surname               = $User.'Last name'
            DisplayName           = $User.'Display name'
            SamAccountName        = $User.'User logon name'
            UserPrincipalName     = $User.'User principal name'
            StreetAddress         = $User.'Street'
            City                  = $User.'City'
            State                 = $User.'State/province'
            PostalCode            = $User.'Zip/Postal Code'
            Country               = $User.'Country/region'
            Title                 = $User.'Job Title'
            Department            = $User.'Department'
            Company               = $User.'Company'
            Manager               = $managerDN
            Path                  = $User.'OU'
            Description           = $User.'Description'
            Office                = $User.'Office'
            OfficePhone           = $User.'Telephone number'
            EmailAddress          = $User.'E-mail'
            MobilePhone           = $User.'Mobile'
            AccountPassword       = (ConvertTo-SecureString "$Password" -AsPlainText -Force)
            Enabled               = if ($User.'Account status' -eq "Enabled") { $true } else { $false }
            ChangePasswordAtLogon = $true # Set the "User must change password at next logon"
        }

        # Add the info attribute to OtherAttributes only if Notes field contains a value
        if (![string]::IsNullOrEmpty($User.Notes)) {
            $NewUserParams.OtherAttributes = @{info = $User.Notes }
        }

        # Check to see if the user already exists in AD
        if (Get-ADUser -Filter "SamAccountName -eq '$($User.'User logon name')'") {

            # Give a warning if user exists
            Write-Host "A user with username $($User.'User logon name') already exists in Active Directory." -ForegroundColor Yellow
        }
        else {
            # User does not exist then proceed to create the new user account
            # Account will be created in the OU provided by the $User.OU variable read from the CSV file
            New-ADUser @NewUserParams
            Write-Host "The user $($User.'User logon name') is created successfully." -ForegroundColor Green
        }
    }
    catch {
        # Handle any errors that occur during account creation
        Write-Host "Failed to create user $($User.'User logon name') - $($_.Exception.Message)" -ForegroundColor Red
    }
}
  • Line 21: Edit the CSV file location.
  • Line 25: Change password.

Step 3: Run import AD users PowerShell script

Run PowerShell as administrator and run the PowerShell script to import AD users from CSV file. Wait till it completes.

C:\scripts\.\Import-ADUsers.ps1

The script will show if:

  1. The user is created successfully.
  2. The user already exists.
  3. The user can’t be created with the error message.
A user with username Kylie.Davidson already exists in Active Directory.
A user with username Leonard.Clark already exists in Active Directory.
A user with username Madeleine.Fisher already exists in Active Directory.
A user with username Melanie.Scott already exists in Active Directory.
A user with username Nicholas.Murray already exists in Active Directory.
A user with username Piers.Bower already exists in Active Directory.
A user with username Ruth.Dickens already exists in Active Directory.
The user Sebastian.Nolan is created successfully.
The user Zoe.Roberts is created successfully.

Step 4: Verify AD users

Verify that the users are successfully created in Active Directory.

Import AD users from CSV with PowerShell ADUC

 

Leave a Reply