Automating Active Directory User Import with PowerShell

In this post, we will explore a useful PowerShell script that automates the process of importing multiple users into Active Directory from a CSV file. This script can streamline the onboarding process and save you time when managing user accounts in your organization.
At ServerEngine, we understand the importance of efficient server management, and our tools can help you maintain a smooth IT operation. Check out our software [ServerEngine](https://serverengine.co) for more powerful management solutions.
### Step 1: Prepare Your CSV File
Before running the script, prepare a CSV file containing user information. The columns should include `FirstName`, `LastName`, `UserName`, and `Password`.
Example `users.csv`:
“`
FirstName,LastName,UserName,Password
John,Doe,jdoe,P@ssw0rd!
Jane,Smith,jsmith,P@ssw0rd!
“`
### Step 2: Import the Required Module
We will start by importing the Active Directory module to ensure we can access the necessary AD cmdlets.
“`powershell

Import-Module ActiveDirectory

### Step 3: Define the Import Function
Next, well create a function called `Import-ADUsers` that reads the CSV file and adds each user to Active Directory.
“`powershell

function Import-ADUsers {
    param (
        [string]$csvPath
    )
    $users = Import-Csv -Path $csvPath
    foreach ($user in $users) {
        $userName = $user.UserName
        $firstName = $user.FirstName
        $lastName = $user.LastName
        $password = $user.Password
        try {
            New-ADUser -Name "$firstName $lastName" -GivenName $firstName -Surname $lastName -SamAccountName $userName -UserPrincipalName "[email protected]" -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -Enabled $true
            Write-Host "Successfully created user: $userName"
        } catch {
            Write-Host "Failed to create user: $userName. Error: $_"
        }
    }
}

### Step 4: Execute the Import
Call the function with the path to your CSV file to start the import process.
“`powershell

Import-ADUsers -csvPath "C:\Path\To\Your\users.csv"

### Conclusion
Using this script, you can simplify the process of adding multiple users to Active Directory. Just ensure you update the CSV file and path appropriately. For more tools and resources to manage your servers efficiently, visit [ServerEngine](https://serverengine.co) and check out our software solutions.