PowerShell to find enabled user ids per OU:

If you’ve got a large domain and want to query user ids per OU, then this is the script for you:
Get enabled user ids per OU:

$filepath=”c:\temp\CompanyUsers.csv”
$outmatrix = @()
$ou=Get-ADOrganizationalUnit -filter * #-searchbase “ou=users,dc=your,dc=domain,dc=name,dc=here” -filter * -searchscope 1
foreach ($o in $ou)
{$count=@(Get-ADUser -searchbase $o -searchscope 1 -filter * |Where-Object {$_.enabled -eq $TRUE}).count
 
#Construct an object
$matrix = “” | Select “ou”, “count”
$matrix.ou = $o
$matrix.count = $count
$outmatrix += $matrix
$matrix = $null
}
$outmatrix |export-csv $filepath -notypeinformation

Leave a Reply