Forum Discussion
sparkislife
Apr 21, 2022Copper Contributor
List off all AD groups that have between 1 and 3 users
I am looking for help with listing off all AD Groups that have between 1 and 3 members.
Apr 24, 2022
Did my example help you?
sparkislife
Apr 25, 2022Copper Contributor
Get-ADGroup -Filter * -Properties Member | Select-Object Name,@{n="MemberCount";e={$_.Member.Count}} |export-csv -NoTypeInformation -Encoding UTF8 -Delimiter ';' -Path C:\temp\groups.csv got me the each group by name and count
Want it filtered to groups with between 1 - 3 users only.
Want it filtered to groups with between 1 - 3 users only.
- Apr 25, 2022
The output looks like the one in my first reply, it lists every group than contains 1,2 or 3 members and gives the amount of members in that group and the member names in a csv. So it does that, not sure what you want?
You can also run this to only show the group and the number of members in it:
$total = @()
foreach ($group in Get-ADGroup -Filter * -ResultSetSize 100000 | Sort-Object Name) {
$members = get-adgroup $group.name | Get-ADGroupMember | Sort-Object Name
$count = @(Get-ADGroupMember -Identity $Group).Count
if ($count -ge 1 -and $count -le 3) {
$info = [PSCustomObject]@{
Group = $group.Name
"Total Members" = $count
}
$total += $info
}
}
$total | export-csv -NoTypeInformation -Encoding UTF8 -Delimiter ';' -Path c:\temp\groups.csv