Skip to main content
These scripts handle the most common AD user management tasks: querying accounts, generating reports, modifying attributes, and managing account status. All examples use standard ActiveDirectory module cmdlets and are compatible with PowerShell 5.1 and later.
Before running any bulk modification script, take an export of the affected accounts first. Use Get-ADUser with Export-Csv to create a backup you can restore from if something goes wrong.

Common parameters

Most user management scripts accept these standard parameters:
ParameterDescriptionExample
-SearchBaseLimits scope to a specific OU"OU=Users,DC=domain,DC=com"
-FilterAD filter expression"Enabled -eq $true"
-PropertiesAdditional attributes to return"Department,Title,LastLogonDate"
-ServerTarget a specific domain controller"dc01.domain.com"

Querying user accounts

Basic queries

# Get a single user by samAccountName
Get-ADUser -Identity jdoe

# Get a user with extended properties
Get-ADUser -Identity jdoe -Properties Department, Title, EmailAddress, LastLogonDate

# Find all users in a specific OU
Get-ADUser -Filter * -SearchBase "OU=Users,DC=domain,DC=com"

# Find all enabled users
Get-ADUser -Filter "Enabled -eq $true" -SearchBase "OU=Users,DC=domain,DC=com"

# Find users who have not logged in within 90 days
$cutoff = (Get-Date).AddDays(-90)
Get-ADUser -Filter "LastLogonDate -lt $cutoff -and Enabled -eq $true" `
    -Properties LastLogonDate

Filtering by department or title

# Find all users in the Nursing department
Get-ADUser -Filter "Department -eq 'Nursing'" -Properties Department, Title

# Find all managers
Get-ADUser -Filter "Title -like '*Manager*'" -Properties Title, Department

Generating user reports

# Full user report exported to CSV
.\ActiveDirectory\Get-ADUserReport.ps1 -SearchBase "OU=Users,DC=domain,DC=com"

# Manual equivalent using standard cmdlets
Get-ADUser -Filter * `
    -SearchBase "OU=Users,DC=domain,DC=com" `
    -Properties DisplayName, EmailAddress, Department, Title, Enabled, LastLogonDate, PasswordLastSet |
    Select-Object DisplayName, SamAccountName, EmailAddress, Department, Title, Enabled, LastLogonDate, PasswordLastSet |
    Export-Csv -Path "C:\Reports\ADUsers_$(Get-Date -Format yyyyMMdd).csv" -NoTypeInformation

Creating and modifying accounts

Create a new user

New-ADUser `
    -Name "Jane Smith" `
    -GivenName "Jane" `
    -Surname "Smith" `
    -SamAccountName "jsmith" `
    -UserPrincipalName "jsmith@domain.com" `
    -Path "OU=Users,OU=Staff,DC=domain,DC=com" `
    -Department "IT" `
    -Title "Systems Administrator" `
    -AccountPassword (ConvertTo-SecureString "TempP@ss1" -AsPlainText -Force) `
    -ChangePasswordAtLogon $true `
    -Enabled $true

Modify a single user’s attributes

Set-ADUser -Identity jsmith `
    -Title "Senior Systems Administrator" `
    -Department "IT Operations" `
    -OfficePhone "555-1234"

Move a user to a different OU

$user = Get-ADUser -Identity jsmith
Move-ADObject -Identity $user.DistinguishedName `
    -TargetPath "OU=Admins,DC=domain,DC=com"

Account status management

# Disable an account
Disable-ADAccount -Identity jdoe

# Enable an account
Enable-ADAccount -Identity jdoe

# Check account status
Get-ADUser -Identity jdoe -Properties Enabled | Select-Object Name, Enabled

# Unlock a locked-out account
Unlock-ADAccount -Identity jdoe

Password management

# Force a password reset at next logon
Set-ADUser -Identity jdoe -ChangePasswordAtLogon $true

# Reset a password programmatically
Set-ADAccountPassword -Identity jdoe `
    -Reset `
    -NewPassword (ConvertTo-SecureString "NewTempP@ss1" -AsPlainText -Force)

# Find accounts with passwords that never expire
Get-ADUser -Filter "PasswordNeverExpires -eq $true" -Properties PasswordNeverExpires |
    Select-Object Name, SamAccountName, PasswordNeverExpires |
    Export-Csv -Path "C:\Reports\NeverExpirePasswords.csv" -NoTypeInformation

Build docs developers (and LLMs) love