← script library

Managing Active Directory Users with PowerShell

Active DirectoryJanuary 5, 2025

In this post, we offer a useful PowerShell script for managing Active Directory (AD) users. Whether you are an IT administrator or a system operator, this script simplifies the process of bulk user creation and can save you significant time.

powershell
# Import Active Directory module
Import-Module ActiveDirectory

# Prepare user data for bulk user creation
$users = @(
    @{
        SamAccountName = jdoe
        GivenName      = John
        Surname        = Doe
        UserPrincipalName = [email protected]
        Password       = P@ssw0rd!
    },
    @{
        SamAccountName = asmith
        GivenName      = Alice
        Surname        = Smith
        UserPrincipalName = [email protected]
        Password       = P@ssw0rd!
    }
)

# Loop through each user and create in Active Directory
foreach ($user in $users) {
    New-ADUser -SamAccountName $user.SamAccountName `
               -GivenName $user.GivenName `
               -Surname $user.Surname `
               -UserPrincipalName $user.UserPrincipalName `
               -Name "$($user.GivenName) $($user.Surname)" `
               -AccountPassword (ConvertTo-SecureString $user.Password -AsPlainText -Force) `
               -Enabled $true
    Write-Host "User $($user.SamAccountName) created successfully."
}

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.