So you want to know how many people are in the groups. As luck would have it, you can get that. There was an interesting thing that I encountered with Get-ADGroupMember when trying to return a count. If there were 0 members, it returned correctly. If there were 2 or more, it returned correctly. If there was 1 member, it returned nothing. It wasn’t null as I checked that. It just returned nothing. Found the answer on this site as to why it was doing it:Why it returned nothing. TLDR: From Martin9700, PowerShell, when only 1 object is returned it is returned AS that object. Count is property of an array (and you can have an array of pretty much any variable/object type)
So with that in mind, I went the route below to do a Measure-Object, then do the count. That returned the results I expected. I also wanted to only select the unique users in the group just in case there were nested groups that a user might have been in more than one of.
# Script to get user group counts. This requires the AD Powershell module, access rights to AD, a central location of Citrix groups, a naming convention, and used in Powershell ISE 5.1.
$domain = "domain"
$searchBase = "OU=CitrixGroups,OU=Groups,DC=somecompany,DC=com"
$getCtxGroups = (Get-ADGroup -server $domain -SearchBase $searchBase -Filter {SamAccountName -like "CitrixGroupNamePattern*"} | Select-Object SamAccountName)
$totalItems = ($getCtxGroups).Count
$date = Get-Date -Format MMddyyyy
$report = @()
$currentItem = 0
$percentComplete = 0
ForEach($ctxGroup in $getCtxGroups){
Write-Progress -Activity "Processing user count on ($ctxGroup).SamAccountName" -Status "$PercentComplete% Complete:" -PercentComplete $PercentComplete
$userCount = (Get-ADGroup -Server $domain $ctxGroup.SamAccountName | Get-ADGroupMember -Recursive | Select-Object -Unique | Measure-Object).Count
$line = "" | Select GroupName, UserCount
$line.GroupName = $ctxGroup.SamAccountName
if($userCount -ne 0){
$line.UserCount = $userCount
}
if($userCount -eq 0){
$line.UserCount = "Empty"
}
$currentItem++
$percentComplete = [int](($currentItem / $totalItems) * 100)
$report += $line
}
$report | Export-Csv c:\scripts\logs\$date-ADUserGroup-Counts.csv -Append -NoTypeInformation