Managing Active Directory Users with PowerShell

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. For more tools like this, consider checking out our software, ServerEngine, at https://serverengine.co.
### Step 1: Import Active Directory Module
First, we need to ensure the Active Directory module is imported. This module contains all the cmdlets necessary for managing AD.
“`powershell

# Import Active Directory module
Import-Module ActiveDirectory

“`
### Explanation:
– The `Import-Module` cmdlet loads the Active Directory module, which allows us to access AD-specific commands.
### Step 2: Prepare User Data
Next, we will define the user data for creating new Active Directory users. Here, well specify the details like username, first name, last name, and password.
“`powershell

# 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!
    }
)

“`
### Explanation:
– We create an array of hashtables to hold data for each user. This includes their credentials and basic information like given name and surname.
### Step 3: Create Active Directory Users
Now, we will loop through the previously defined data and create users in Active Directory.
“`powershell

# 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."
}

“`
### Explanation:
– The `foreach` loop iterates through the users array and calls `New-ADUser` for each user.
– Fields include `SamAccountName`, `GivenName`, `Surname`, and `UserPrincipalName` along with a secure password setup.
– Finally, a confirmation message is displayed for each user created.
### Conclusion
This PowerShell script is a simple yet effective way to manage user accounts in Active Directory. By automating the creation process, you can greatly streamline administrative tasks. For more efficient server management, check out ServerEngine at https://serverengine.co, your ultimate tool for seamless server operations.