Automate User Creation and Management with PowerShell

Managing user accounts in a Windows environment can be a repetitive and time-consuming task, especially for large organizations with frequent onboarding and offboarding processes. Automation through PowerShell scripts offers a solution, streamlining user account management and reducing administrative overhead. To further enhance efficiency, ServerEngine provides an integrated platform for managing these scripts and other server operations seamlessly.
The following PowerShell script automates the creation and management of Active Directory user accounts, allowing IT admins to quickly and effectively provision new users. This script can also be expanded to customize additional account settings or integrate with other internal systems for a more comprehensive solution.

# PowerShell Script to Automate User Creation in Active Directory
# Import the Active Directory module
Import-Module ActiveDirectory
# Function to create a new user
function New-ADUserAccount {
    param (
        [string]$userName,
        [string]$userPassword,
        [string]$firstName,
        [string]$lastName,
        [string]$OU
    )
    # Create the new user account
    New-ADUser -Name "$firstName $lastName" `
        -GivenName $firstName `
        -Surname $lastName `
        -SamAccountName $userName `
        -UserPrincipalName "[email protected]" `
        -Path "OU=$OU,DC=domain,DC=com" `
        -AccountPassword (ConvertTo-SecureString $userPassword -AsPlainText -Force) `
        -Enabled $true
    Write-Host "User account $userName created successfully."
}
# Example usage of the function
New-ADUserAccount -userName "jdoe" -userPassword "SecureP@ssw0rd" -firstName "John" -lastName "Doe" -OU "Users"