Kae Travis

Count members of an AD Group and write to a CSV

This post provides an example of how to count members of an AD Group and write to a CSV.

This script finds all AD groups beginning with ‘MSI – ‘ or ‘App-V – ‘ and returns the number of members it contains. Note that it doesn’t recurse nested groups! It then outputs the results to a CSV file.

$csvPath = "C:\Alkane\AlkaneGroups.csv"
"Group,Count" | Out-File -filepath $csvPath -Append -encoding ASCII
$groups = Get-ADGroup -filter {(name -like "MSI - *") -or (name -like "App-V - *")}
foreach($group in $groups){
$countUser = (Get-ADGroup $group -Properties *).member.count
Write-Host "The group $($group.Name) has $countUser user(s)."
"""$($group.Name)"",$countUser" | Out-File -filepath $csvPath -Append -encoding ASCII
}

 

Count members of an AD Group and write to a CSV
Count members of an AD Group and write to a CSV

Leave a Reply