<# .SYNOPSIS Generates an Active Directory user report. .DESCRIPTION Creates a user report from Active Directory that includes enabled status, department, title, email address, last logon date, password last set date, and account creation date. This script is useful for account reviews, cleanup projects, audit preparation, and general Active Directory administration. The script does not contain hardcoded user names or server names. .EXAMPLE .\Get-ADUserReport.ps1 Generates a report for all users in Active Directory and displays the results. .EXAMPLE 2 .\Get-ADUserReport.ps1 -SearchBase "OU=CorporateUsers,DC=planetbradford,DC=com" Generates a report for users in a specific OU. I typically use this when I only want to target a department, user OU, cleanup project, or account review instead of querying the entire directory. .EXAMPLE 3 .\Get-ADUserReport.ps1 -SearchBase "OU=CorporateUsers,DC=planetbradford,DC=com" -ExportCsv .\ADUserReport.csv Generates the report for a specific OU and exports it to a CSV file. #> [CmdletBinding()] param ( [string]$SearchBase, [string]$ExportCsv ) try { Import-Module ActiveDirectory -ErrorAction Stop } catch { Write-Error "The Active Directory PowerShell module could not be loaded. Run this from a system with RSAT or AD tools installed." exit 1 } try { $Properties = @( "DisplayName", "Enabled", "Department", "Title", "Mail", "LastLogonDate", "PasswordLastSet", "Created" ) if ($SearchBase) { $Users = Get-ADUser -Filter * -SearchBase $SearchBase -Properties $Properties -ErrorAction Stop } else { $Users = Get-ADUser -Filter * -Properties $Properties -ErrorAction Stop } $Report = $Users | Select-Object ` SamAccountName, DisplayName, Enabled, Department, Title, Mail, LastLogonDate, PasswordLastSet, Created | Sort-Object DisplayName if ($ExportCsv) { $Report | Export-Csv -Path $ExportCsv -NoTypeInformation Write-Host "Report exported to $ExportCsv" -ForegroundColor Green } else { $Report | Format-Table -AutoSize } } catch { Write-Error "Failed to generate Active Directory user report. Error: $($_.Exception.Message)" }