Active Directory · READ-ONLY / LOCAL CSV · v1.0.0
AD user roster status
Check a supplied account list for enabled, disabled, missing, or failed lookups in one OU.
Offline tests do not establish live directory behavior. Review the source and run first in a small authorized lab. Nothing runs in this browser.
What you’ll need
- PowerShell on Windows with the ActiveDirectory (RSAT) module.
- Read access to the selected objects and required attributes on the chosen domain controller.
Run locally
Replace fictional inputs with values approved for your environment. Use an access-controlled local folder for output.
./Export-SwpADUserRosterStatus.ps1 -Server dc01.example.test -SearchBase 'OU=People,DC=example,DC=test' -InputPath ./accounts.txt -OutputPath ./roster.csvRead the results
| Field | Meaning |
|---|---|
SamAccountName | The supplied user or returned group name. |
Status | Enabled, Disabled, Unknown, NotFound, or LookupFailed. |
CreatedUtc | Object creation time in UTC, when returned. |
ChangedUtc | Last recorded object change time in UTC, when returned. Not a sign-in time. |
- Supply one fictional or approved sAMAccountName per line; for example, demo-user. Blank lines are ignored.
- NotFound means no visible match in the selected OU. A failed batch is LookupFailed, never NotFound.
- Run against a test roster first. For larger runs, adjust the 100-account default via -BatchSize.
Keep the output local and protected. A successful report does not prove complete directory visibility; do not use it by itself to change or remove accounts or groups.
Formula-like text cells are prefixed with an apostrophe. Keep this in mind when joining exports by identifier.
Complete source
The display, clipboard copy, download and GitHub link refer to the same pinned source.
<#
.SYNOPSIS
Reports AD lookup and account state for a supplied list of sAMAccountNames.
.DESCRIPTION
Queries one explicit domain controller and OU with bounded LDAP batches.
Writes a new local CSV. NotFound means no visible match in the selected OU.
A failed batch is LookupFailed, never NotFound. Treat output as sensitive.
Version 1.0.0.
.EXAMPLE
./Export-SwpADUserRosterStatus.ps1 -Server dc01.example.test -SearchBase 'OU=People,DC=example,DC=test' -InputPath ./accounts.txt -OutputPath ./roster.csv
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $Server,
[Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $SearchBase,
[Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $InputPath,
[Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $OutputPath,
[ValidateRange(1, 500)] [int] $BatchSize = 100
)
$ErrorActionPreference = 'Stop'
Import-Module ActiveDirectory
function Protect-Cell([string] $Value) {
if ($Value -match '^[\s\x00-\x1f]*[=+@-]|^[\t\r\n]') { return "'" + $Value }
return $Value
}
$names = @(Get-Content -LiteralPath $InputPath | ForEach-Object { $_.Trim() } |
Where-Object { $_ } | Select-Object -Unique)
if (!$names.Count) { throw 'Provide at least one sAMAccountName, one per line.' }
if (Test-Path -LiteralPath $OutputPath) { throw 'Choose a new output file.' }
$results = for ($offset = 0; $offset -lt $names.Count; $offset += $BatchSize) {
$batch = @($names[$offset..([Math]::Min($offset + $BatchSize, $names.Count) - 1)])
$parts = foreach ($name in $batch) {
$escaped = $name.Replace('\', '\5c').Replace('*', '\2a').Replace('(', '\28').Replace(')', '\29').Replace([string][char]0, '\00')
'(sAMAccountName=' + $escaped + ')'
}
$found = @{}
$failed = $false
try {
$filter = '(|' + ($parts -join '') + ')'
$users = @(Get-ADUser -LDAPFilter $filter -Server $Server -SearchBase $SearchBase -SearchScope Subtree -ResultPageSize 200 -Properties whenCreated, whenChanged)
foreach ($user in $users) { $found[$user.SamAccountName] = $user }
} catch {
$failed = $true
Write-Warning ('AD lookup failed for a batch of {0} account(s). Check connectivity and permissions.' -f $batch.Count)
}
foreach ($name in $batch) {
$user = $found[$name]
$status = if ($failed) { 'LookupFailed' } elseif (!$user) { 'NotFound' } elseif ($null -eq $user.Enabled) { 'Unknown' } elseif ($user.Enabled) { 'Enabled' } else { 'Disabled' }
[pscustomobject]@{
SamAccountName = Protect-Cell $name
Status = $status
CreatedUtc = if (!$failed -and $null -ne $user.whenCreated) { $user.whenCreated.ToUniversalTime().ToString('o') }
ChangedUtc = if (!$failed -and $null -ne $user.whenChanged) { $user.whenChanged.ToUniversalTime().ToString('o') }
}
}
}
$results | Export-Csv -LiteralPath $OutputPath -NoTypeInformation -Encoding UTF8 -NoClobber
Write-Host ('Exported {0} roster result(s).' -f @($results).Count)