skip to content

Search

Syspirit
EN

Active Directory

Active Directory administration with PowerShell - users, groups, and daily tasks!

Windows
Published on

Active Directory is Microsoft’s directory service for centralized management of users, computers, and resources in a Windows Server environment.

📦 Module Preparation

📌 Action🧠 PowerShell Command
📦 Import AD moduleImport-Module ActiveDirectory
✅ Verify moduleGet-Module ActiveDirectory
🔍 List AD cmdletsGet-Command -Module ActiveDirectory
📋 Cmdlet helpGet-Help Get-ADUser -Examples

👥 User Management

📌 Action🧠 PowerShell Command
📋 List usersGet-ADUser -Filter *
🔍 Search userGet-ADUser -Identity "jsmith"
👁️ Full detailsGet-ADUser "jsmith" -Properties *
🔍 Search by nameGet-ADUser -Filter "Name -like '*Smith*'"
👤 Create userNew-ADUser -Name "John Smith" -SamAccountName "jsmith"
🔧 Modify userSet-ADUser -Identity "jsmith" -Description "IT Admin"
🔒 Disable accountDisable-ADAccount -Identity "jsmith"
✅ Enable accountEnable-ADAccount -Identity "jsmith"
🗑️ Delete userRemove-ADUser -Identity "jsmith"

👥 Group Management

📌 Action🧠 PowerShell Command
📋 List groupsGet-ADGroup -Filter *
🔍 Search groupGet-ADGroup -Identity "Admins"
👁️ Group membersGet-ADGroupMember -Identity "Admins"
➕ Add memberAdd-ADGroupMember -Identity "Admins" -Members "jsmith"
➖ Remove memberRemove-ADGroupMember -Identity "Admins" -Members "jsmith"
👤 User’s groupsGet-ADUser "jsmith" -Properties MemberOf
🆕 Create groupNew-ADGroup -Name "IT-Team" -GroupScope Global

💻 Computer Management

📌 Action🧠 PowerShell Command
📋 List computersGet-ADComputer -Filter *
🔍 Search PCGet-ADComputer -Identity "PC-001"
📊 PC with detailsGet-ADComputer "PC-001" -Properties *
🆕 Add PCNew-ADComputer -Name "PC-002" -Path "OU=Computers,DC=domain,DC=com"
🗑️ Delete PCRemove-ADComputer -Identity "PC-001"
⏰ Last logonGet-ADComputer -Filter * -Properties LastLogonDate

🔍 Advanced Searches

🎯 Inactive users

# Users not logged in for 90 days
$date = (Get-Date).AddDays(-90)
Get-ADUser -Filter * -Properties LastLogonDate | Where-Object {$_.LastLogonDate -lt $date}

🔒 Disabled accounts

Get-ADUser -Filter {Enabled -eq $false}

👥 Users from a specific group

Get-ADGroupMember -Identity "Domain Admins" | Get-ADUser -Properties DisplayName

🏢 Organizational Structure (OU)

📌 Action🧠 PowerShell Command
📋 List OUsGet-ADOrganizationalUnit -Filter *
🆕 Create OUNew-ADOrganizationalUnit -Name "IT" -Path "DC=domain,DC=com"
📦 OU contentsGet-ADObject -SearchBase "OU=IT,DC=domain,DC=com" -Filter *
🔄 Move userMove-ADObject -Identity "CN=jsmith,..." -TargetPath "OU=IT,..."

🛠️ Script Examples

📊 Export a group with its members to Excel:

# Define the group name to analyze
$groupName = "GRP-accounting"
 
# Get the group and its members from the domain
$group = Get-ADGroup -Identity $groupName -Properties Members
 
# For each group member, retrieve full details
$members = $group.Members | ForEach-Object {
    # Get-ADObject retrieves all AD object types (users, computers, etc.)
    Get-ADObject -Identity $_ -Properties *
}
 
# Display a preview of members in the console
$members | Format-Table Name, ObjectClass, DistinguishedName
 
# Build the filename with today's date
$excelFilePath = "C:\folder\export_$($groupName)_$(Get-Date -Format 'yyyyMMdd').xlsx"
 
# Export to Excel (requires ImportExcel module)
$members | Select-Object Name, ObjectClass, DistinguishedName |
    Export-Excel -Path $excelFilePath -AutoSize
 
Write-Host "Export completed: '$excelFilePath'"

🔍 User account audit

# User report with essential info
Get-ADUser -Filter * -Properties DisplayName, LastLogonDate, PasswordLastSet |
    Select-Object Name, DisplayName, Enabled, LastLogonDate, PasswordLastSet |
    Export-Csv "C:\temp\users_audit.csv" -NoTypeInformation

👥 Groups and members report

# List all groups with member count
Get-ADGroup -Filter * | ForEach-Object {
    $memberCount = (Get-ADGroupMember -Identity $_ -ErrorAction SilentlyContinue).Count
    [PSCustomObject]@{
        GroupName = $_.Name
        MemberCount = $memberCount
        GroupScope = $_.GroupScope
    }
} | Sort-Object MemberCount -Descending

🔒 Problematic accounts

# Expired or locked accounts
Get-ADUser -Filter * -Properties LockedOut, PasswordExpired, AccountExpirationDate |
    Where-Object {$_.LockedOut -eq $true -or $_.PasswordExpired -eq $true} |
    Select-Object Name, LockedOut, PasswordExpired, AccountExpirationDate

📈 Monitoring and Reports

📊 Domain statistics

# Domain overview
Write-Host "=== Active Directory Statistics ===" -ForegroundColor Green
Write-Host "Total users: $((Get-ADUser -Filter *).Count)"
Write-Host "Total groups: $((Get-ADGroup -Filter *).Count)"
Write-Host "Total computers: $((Get-ADComputer -Filter *).Count)"
Write-Host "Total OUs: $((Get-ADOrganizationalUnit -Filter *).Count)"

⚠️ Security alerts

# Users with password never expires
Get-ADUser -Filter {PasswordNeverExpires -eq $true} -Properties PasswordNeverExpires |
    Select-Object Name, PasswordNeverExpires

🚨 Common Troubleshooting

🆘 Problem🧠 Solution
Missing AD moduleInstall-WindowsFeature RSAT-AD-PowerShell
Insufficient permissionsUse a domain admin account
DC connection errorSpecify -Server "dc.domain.com"
Excel export failsInstall Install-Module ImportExcel
Slow searchUse -SearchBase "OU=..." to limit scope