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?
- sparkislifeApr 25, 2022Copper ContributorGet-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.- 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
- sparkislifeApr 25, 2022Copper Contributor
- Apr 25, 2022Ok, thats strange... After running the script, did it do anything? Did it create a c:\temp\groups.csv file? Does running Get-ADGroup -Filter * -ResultSetSize 100000 | Sort-Object Name give you any results?
- sparkislifeApr 25, 2022Copper Contributor
I am looking to get almost a thousand groups. Am sorry I had to abort it as it kept throwing the unspecified error at me.
I went this route so far.
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