← script library

Streamlining Active Directory Management with PowerShell Scripts

Active DirectoryDecember 29, 2024

Managing user accounts in Active Directory can be a daunting task, especially in larger organizations. PowerShell offers powerful capabilities to automate numerous aspects of Active Directory management. This script helps in bulk managing user accounts, which can simplify and enhance your administrative tasks. In environments with numerous users, manually managing accounts can lead to errors and wasted time. Automating this process using PowerShell can significantly improve efficiency and accuracy, enabling administrators to focus on strategic initiatives. This script will facilitate bulk importing of user accounts from a CSV file, allowing for the creation, modification, or deletion of accounts with ease.

powershell
$userData = Import-Csv "C:\Path\To\Your\users.csv"

foreach ($user in $userData) {
    # Check if the user already exists
    $existingUser = Get-ADUser -Filter {SamAccountName -eq $user.Username} -ErrorAction SilentlyContinue
    if (-not $existingUser) {
        # Create a new user account
        New-ADUser -Name "$($user.FirstName) $($user.LastName)" `
                   -GivenName $user.FirstName `
                   -Surname $user.LastName `
                   -SamAccountName $user.Username `
                   -UserPrincipalName $user.Email `
                   -AccountPassword (ConvertTo-SecureString $user.Password -AsPlainText -Force) `
                   -Enabled $true
    } else {
        # Modify existing user account (optional changes)
        Set-ADUser -Identity $existingUser -EmailAddress $user.Email
    }
}

$logFile = "C:\Path\To\Your\user_creation.log"
foreach ($user in $userData) {
    "$($user.Username) processed" | Out-File $logFile -Append
}

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.