Find the Membership Count of all AD Groups in an OU

I was working on an application migration recently, and wanted to see how many users/computers were in each application Active Directory (AD) group in a specified Organisational Unit (OU). I wrote a PowerShell script to find the membership count of all AD Groups in an OU:

$Groups = Get-ADGroup -Properties * -Filter * -SearchBase "OU=Applications,DC=alkane,DC=co,DC=uk" 
Foreach($group In $groups)
{
$groupname = $group.Name 
$groupdescription = $group.Description
$groupdn = $group.DistinguishedName
$groupcount = $group.member.count
}

 

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
}