List off all AD groups that have between 1 and 3 users

%3CLINGO-SUB%20id%3D%22lingo-sub-3291828%22%20slang%3D%22en-US%22%3EList%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3291828%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20looking%20for%20help%20with%20listing%20off%20all%20AD%20Groups%20that%20have%20between%201%20and%203%20members.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3291828%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EActive%20Directory%20(AD)%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPoweShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3297781%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3297781%22%20slang%3D%22en-US%22%3E%3CP%3Eworked%20fine%20thx.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3297779%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3297779%22%20slang%3D%22en-US%22%3EYes%20that%20did%20the%20trick.%20Many%20thanks%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3294640%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3294640%22%20slang%3D%22en-US%22%3ENot%20sure%20why%20the%20error%20is%20there%20in%20your%20case%2C%20it%20does%20work%20on%20a%202022%20DC%20with%20not%20that%20many%20groups%20but%20should%20work%20with%20a%20lot%20more%20groups%20without%20any%20problems.%20%40LianRobertson%20posted%20an%20adjustment%20to%20your%20line%20below%20which%20should%20work%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3294480%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3294480%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CBLOCKQUOTE%3E%3CHR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1370084%22%20target%3D%22_blank%22%3E%40sparkislife%3C%2FA%3E%26nbsp%3Bwrote%3A%3CBR%20%2F%3ELooking%20at%20explicit%20and%20am%20looking%20for%20user%20count%20per%20group.%3CHR%20%2F%3E%3C%2FBLOCKQUOTE%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1370084%22%20target%3D%22_blank%22%3E%40sparkislife%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOkay%2C%20in%20this%20case%2C%20something%20like%20this%20will%20suffice.%20You%20can%20add%20and%20remove%20any%20attributes%20you'd%20like.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EGet-ADObject%20-Filter%20%7B%20(objectClass%20-eq%20%22group%22)%20-and%20(member%20-like%20%22*%22)%20%7D%20-Properties%20member%20%7C%20Select-Object%20-Property%20objectGUID%2C%20name%2C%20%40%7Bn%3D%22count%22%3B%20e%3D%7B%20%24_.member.Count%3B%20%7D%7D%20%7C%20Where-Object%20%7B%20%24_.Count%20-in%201%20..%203%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheers%2C%3C%2FP%3E%3CP%3ELain%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3294479%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3294479%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20looking%20to%20get%20almost%20a%20thousand%20groups.%20Am%20sorry%20I%20had%20to%20abort%20it%20as%20it%20kept%20throwing%20the%20unspecified%20error%20at%20me.%3CBR%20%2F%3EI%20went%20this%20route%20so%20far.%3CBR%20%2F%3EGet-ADGroup%20-Filter%20*%20-Properties%20Member%20%7C%20Select-Object%20Name%2C%40%7Bn%3D%22MemberCount%22%3Be%3D%7B%24_.Member.Count%7D%7D%20%7Cexport-csv%20-NoTypeInformation%20-Encoding%20UTF8%20-Delimiter%20'%3B'%20-Path%20C%3A%5Ctemp%5Cgroups.csv%20got%20me%20the%20each%20group%20by%20name%20and%20count%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3294249%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3294249%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20output%20looks%20like%20the%20one%20in%20my%20first%20reply%2C%20it%20lists%20every%20group%20than%20contains%201%2C2%20or%203%20members%20and%20gives%20the%20amount%20of%20members%20in%20that%20group%20and%20the%20member%20names%20in%20a%20csv.%20So%20it%20does%20that%2C%20not%20sure%20what%20you%20want%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20also%20run%20this%20to%20only%20show%20the%20group%20and%20the%20number%20of%20members%20in%20it%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24total%20%3D%20%40()%3CBR%20%2F%3Eforeach%20(%24group%20in%20Get-ADGroup%20-Filter%20*%20-ResultSetSize%20100000%20%7C%20Sort-Object%20Name)%20%7B%3CBR%20%2F%3E%24members%20%3D%20get-adgroup%20%24group.name%20%7C%20Get-ADGroupMember%20%7C%20Sort-Object%20Name%3CBR%20%2F%3E%24count%20%3D%20%40(Get-ADGroupMember%20-Identity%20%24Group).Count%3CBR%20%2F%3Eif%20(%24count%20-ge%201%20-and%20%24count%20-le%203)%20%7B%3CBR%20%2F%3E%24info%20%3D%20%5BPSCustomObject%5D%40%7B%3CBR%20%2F%3EGroup%20%3D%20%24group.Name%3CBR%20%2F%3E%22Total%20Members%22%20%3D%20%24count%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%24total%20%2B%3D%20%24info%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%24total%20%7C%20export-csv%20-NoTypeInformation%20-Encoding%20UTF8%20-Delimiter%20'%3B'%20-Path%20c%3A%5Ctemp%5Cgroups.csv%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3294242%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3294242%22%20slang%3D%22en-US%22%3EGet-ADGroup%20-Filter%20*%20-Properties%20Member%20%7C%20Select-Object%20Name%2C%40%7Bn%3D%22MemberCount%22%3Be%3D%7B%24_.Member.Count%7D%7D%20%7Cexport-csv%20-NoTypeInformation%20-Encoding%20UTF8%20-Delimiter%20'%3B'%20-Path%20C%3A%5Ctemp%5Cgroups.csv%20got%20me%20the%20each%20group%20by%20name%20and%20count%3CBR%20%2F%3EWant%20it%20filtered%20to%20groups%20with%20between%201%20-%203%20users%20only.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3294207%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3294207%22%20slang%3D%22en-US%22%3ELooking%20at%20explicit%20and%20am%20looking%20for%20user%20count%20per%20group.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3294199%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3294199%22%20slang%3D%22en-US%22%3EOk%2C%20thats%20strange...%20After%20running%20the%20script%2C%20did%20it%20do%20anything%3F%20Did%20it%20create%20a%20c%3A%5Ctemp%5Cgroups.csv%20file%3F%20Does%20running%20Get-ADGroup%20-Filter%20*%20-ResultSetSize%20100000%20%7C%20Sort-Object%20Name%20give%20you%20any%20results%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3294187%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3294187%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1209009%22%20target%3D%22_blank%22%3E%40Harm_Veenstra%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESorry%20had%20a%20busier%20weekend.%20Many%20thanks%20but%20lines%203%20and%204%20throw%20the%20error%20below.%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22sparkislife_0-1650907488150.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F366614i1D637109B886B1AB%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22sparkislife_0-1650907488150.png%22%20alt%3D%22sparkislife_0-1650907488150.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3293348%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3293348%22%20slang%3D%22en-US%22%3EDid%20my%20example%20help%20you%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3292098%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3292098%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F314983%22%20target%3D%22_blank%22%3E%40LainRobertson%3C%2FA%3E%26nbsp%3BIt%20was%20the%20most%20simple%20one%20to%20get%20a%20list%20of%20groups%20which%20match%20the%201-3%20member%20requirement.%20But%20the%20get-adgroupmember%20also%20has%20a%20-recursive%20parameter%20which%20lists%20all%20nested%20membership%20too.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3292090%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3292090%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1209009%22%20target%3D%22_blank%22%3E%40Harm_Veenstra%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt'd%20be%20useful%20to%20know%20from%20the%20poster%20if%20this%20is%20explicit%20or%20transitive%2C%20and%20related%20to%20members%20that%20are%20users%20or%20of%20any%20object%20type.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20couldn't%20motivate%20myself%20to%20ask%20but%20they're%20all%20things%20that%20would%20definitely%20impact%20the%20script.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere's%20a%20quick%20one-liner%20that%20demonstrates%20pulling%20the%20transitive%20membership%2C%20which%20requires%20an%20additional%20round%20trip%20per%20group%20(since%20it's%20only%20available%20on%20base%20searches)%20and%20the%20explicit%20requesting%20of%20the%20msds-memberTransitive%20constructed%20attribute%20(i.e.%20using%20%22-Properties%20*%22%20doesn't%20cut%20it.)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E(Get-ADGroup%20-SearchBase%20((Get-ADGroup%20-Identity%20%22Domain%20Admins%22).distinguishedName)%20-SearchScope%20Base%20-Filter%20*%20-Properties%20msds-memberTransitive).'msds-memberTransitive'.Count%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnyhow%2C%20that's%20all%20from%20me%20as%20the%20question's%20too%20open%20to%20know%20how%20best%20to%20answer%20at%20the%20moment.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheers%2C%3C%2FP%3E%3CP%3ELain%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3292024%22%20slang%3D%22en-US%22%3ERe%3A%20List%20off%20all%20AD%20groups%20that%20have%20between%201%20and%203%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3292024%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1370084%22%20target%3D%22_blank%22%3E%40sparkislife%3C%2FA%3E%26nbsp%3BI%20made%20this%20in%20my%20test%20DC%2C%20it%20will%20create%20a%20list%20of%20all%20groups%20containing%201%2C2%20or%203%20members%20and%20output%20them%20to%20c%3A%5Ctemp%5Cgroups.csv%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24total%20%3D%20%40()%0Aforeach%20(%24group%20in%20Get-ADGroup%20-Filter%20*%20-ResultSetSize%20100000%20%7C%20Sort-Object%20Name)%20%7B%0A%20%20%20%20%24members%20%3D%20get-adgroup%20%24group.name%20%7C%20Get-ADGroupMember%20%7C%20Sort-Object%20Name%0A%20%20%20%20%24count%20%3D%20%40(Get-ADGroupMember%20-Identity%20%24Group).Count%0A%20%20%20%20if%20(%24count%20-ge%201%20-and%20%24count%20-le%203)%20%7B%0A%20%20%20%20%20%20%20%20foreach%20(%24member%20in%20%24members)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24info%20%3D%20%5BPSCustomObject%5D%40%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Group%20%20%20%20%20%20%20%20%20%20%20%3D%20%24group.Name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Total%20Members%22%20%3D%20%24count%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Member%20%20%20%20%20%20%20%20%20%20%3D%20%24member.SamAccountName%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%24total%20%2B%3D%20%24info%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%24total%20%7C%20export-csv%20-NoTypeInformation%20-Encoding%20UTF8%20-Delimiter%20'%3B'%20-Path%20c%3A%5Ctemp%5Cgroups.csv%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOutput%20looks%20like%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%22Group%22%3B%22Total%20Members%22%3B%22Member%22%3CBR%20%2F%3E%22Administrators%22%3B%223%22%3B%22Administrator%22%3CBR%20%2F%3E%22Administrators%22%3B%223%22%3B%22Domain%20Admins%22%3CBR%20%2F%3E%22Administrators%22%3B%223%22%3B%22Enterprise%20Admins%22%3CBR%20%2F%3E%22Cert%20Publishers%22%3B%221%22%3B%22W2K22DC%24%22%3CBR%20%2F%3E%22Certificate%20Service%20DCOM%20Access%22%3B%221%22%3B%22Authenticated%20Users%22%3CBR%20%2F%3E%22Domain%20Admins%22%3B%221%22%3B%22Administrator%22%3CBR%20%2F%3E%22Domain%20Controllers%22%3B%221%22%3B%22W2K22DC%24%22%3CBR%20%2F%3E%22Domain%20Guests%22%3B%221%22%3B%22Guest%22%3CBR%20%2F%3E%22Enterprise%20Admins%22%3B%221%22%3B%22Administrator%22%3CBR%20%2F%3E%22Group%201%22%3B%221%22%3B%22test.user1%22%3CBR%20%2F%3E%22Group%202%22%3B%222%22%3B%22test.user1%22%3CBR%20%2F%3E%22Group%202%22%3B%222%22%3B%22test.user2%22%3CBR%20%2F%3E%22Group%20Policy%20Creator%20Owners%22%3B%221%22%3B%22Administrator%22%3CBR%20%2F%3E%22Guests%22%3B%222%22%3B%22Domain%20Guests%22%3CBR%20%2F%3E%22Guests%22%3B%222%22%3B%22Guest%22%3CBR%20%2F%3E%22Helpdesk%22%3B%223%22%3B%22test.user1%22%3CBR%20%2F%3E%22Helpdesk%22%3B%223%22%3B%22test.user2%22%3CBR%20%2F%3E%22Helpdesk%22%3B%223%22%3B%22test.user3%22%3CBR%20%2F%3E%22Pre-Windows%202000%20Compatible%20Access%22%3B%222%22%3B%22Authenticated%20Users%22%3CBR%20%2F%3E%22Pre-Windows%202000%20Compatible%20Access%22%3B%222%22%3B%22W2K22DC%24%22%3CBR%20%2F%3E%22RDS%20Endpoint%20Servers%22%3B%222%22%3B%22NETWORK%20SERVICE%22%3CBR%20%2F%3E%22RDS%20Endpoint%20Servers%22%3B%222%22%3B%22W2K22DC%24%22%3CBR%20%2F%3E%22RDS%20Management%20Servers%22%3B%222%22%3B%22NETWORK%20SERVICE%22%3CBR%20%2F%3E%22RDS%20Management%20Servers%22%3B%222%22%3B%22W2K22DC%24%22%3CBR%20%2F%3E%22RDS%20Remote%20Access%20Servers%22%3B%221%22%3B%22W2K22DC%24%22%3CBR%20%2F%3E%22Remote%20Desktop%20Users%22%3B%221%22%3B%22Domain%20Users%22%3CBR%20%2F%3E%22Schema%20Admins%22%3B%221%22%3B%22Administrator%22%3CBR%20%2F%3E%22Terminal%20Server%20License%20Servers%22%3B%222%22%3B%22NETWORK%20SERVICE%22%3CBR%20%2F%3E%22Terminal%20Server%20License%20Servers%22%3B%222%22%3B%22W2K22DC%24%22%3CBR%20%2F%3E%22Users%22%3B%223%22%3B%22Authenticated%20Users%22%3CBR%20%2F%3E%22Users%22%3B%223%22%3B%22Domain%20Users%22%3CBR%20%2F%3E%22Users%22%3B%223%22%3B%22INTERACTIVE%22%3CBR%20%2F%3E%22Windows%20Authorization%20Access%20Group%22%3B%221%22%3B%22ENTERPRISE%20DOMAIN%20CONTROLLERS%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20was%20a%20bug%2Fglitch%20listing%20groups%20with%20only%201%20member%2C%20but%20this%20article%20fixed%20that%20for%20me%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.spiceworks.com%2Ftopic%2F2237955-more-different-results-get-adgroupmember-count-null%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fcommunity.spiceworks.com%2Ftopic%2F2237955-more-different-results-get-adgroupmember-count-null%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I am looking for help with listing off all AD Groups that have between 1 and 3 members.

14 Replies

@sparkislife I made this in my test DC, it will create a list of all groups containing 1,2 or 3 members and output them to c:\temp\groups.csv

 

 

$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) {
        foreach ($member in $members) {
            $info = [PSCustomObject]@{
                Group           = $group.Name
                "Total Members" = $count
                Member          = $member.SamAccountName
            }
            $total += $info
        }
    }
}
$total | export-csv -NoTypeInformation -Encoding UTF8 -Delimiter ';' -Path c:\temp\groups.csv

 

 

Output looks like:

 

"Group";"Total Members";"Member"
"Administrators";"3";"Administrator"
"Administrators";"3";"Domain Admins"
"Administrators";"3";"Enterprise Admins"
"Cert Publishers";"1";"W2K22DC$"
"Certificate Service DCOM Access";"1";"Authenticated Users"
"Domain Admins";"1";"Administrator"
"Domain Controllers";"1";"W2K22DC$"
"Domain Guests";"1";"Guest"
"Enterprise Admins";"1";"Administrator"
"Group 1";"1";"test.user1"
"Group 2";"2";"test.user1"
"Group 2";"2";"test.user2"
"Group Policy Creator Owners";"1";"Administrator"
"Guests";"2";"Domain Guests"
"Guests";"2";"Guest"
"Helpdesk";"3";"test.user1"
"Helpdesk";"3";"test.user2"
"Helpdesk";"3";"test.user3"
"Pre-Windows 2000 Compatible Access";"2";"Authenticated Users"
"Pre-Windows 2000 Compatible Access";"2";"W2K22DC$"
"RDS Endpoint Servers";"2";"NETWORK SERVICE"
"RDS Endpoint Servers";"2";"W2K22DC$"
"RDS Management Servers";"2";"NETWORK SERVICE"
"RDS Management Servers";"2";"W2K22DC$"
"RDS Remote Access Servers";"1";"W2K22DC$"
"Remote Desktop Users";"1";"Domain Users"
"Schema Admins";"1";"Administrator"
"Terminal Server License Servers";"2";"NETWORK SERVICE"
"Terminal Server License Servers";"2";"W2K22DC$"
"Users";"3";"Authenticated Users"
"Users";"3";"Domain Users"
"Users";"3";"INTERACTIVE"
"Windows Authorization Access Group";"1";"ENTERPRISE DOMAIN CONTROLLERS"

 

There was a bug/glitch listing groups with only 1 member, but this article fixed that for me https://community.spiceworks.com/topic/2237955-more-different-results-get-adgroupmember-count-null

@Harm_Veenstra 

 

It'd be useful to know from the poster if this is explicit or transitive, and related to members that are users or of any object type.

 

I couldn't motivate myself to ask but they're all things that would definitely impact the script.

 

Here's a quick one-liner that demonstrates pulling the transitive membership, which requires an additional round trip per group (since it's only available on base searches) and the explicit requesting of the msds-memberTransitive constructed attribute (i.e. using "-Properties *" doesn't cut it.)

 

(Get-ADGroup -SearchBase ((Get-ADGroup -Identity "Domain Admins").distinguishedName) -SearchScope Base -Filter * -Properties msds-memberTransitive).'msds-memberTransitive'.Count

 

Anyhow, that's all from me as the question's too open to know how best to answer at the moment.

 

Cheers,

Lain

@LainRobertson It was the most simple one to get a list of groups which match the 1-3 member requirement. But the get-adgroupmember also has a -recursive parameter which lists all nested membership too.

Did my example help you?

@Harm_Veenstra 

Sorry had a busier weekend. Many thanks but lines 3 and 4 throw the error below. 

sparkislife_0-1650907488150.png

 

Ok, 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?
Looking at explicit and am looking for user count per group.
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.

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

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

 


@sparkislife wrote:
Looking at explicit and am looking for user count per group.

@sparkislife 

 

Okay, in this case, something like this will suffice. You can add and remove any attributes you'd like.

 

Get-ADObject -Filter { (objectClass -eq "group") -and (member -like "*") } -Properties member | Select-Object -Property objectGUID, name, @{n="count"; e={ $_.member.Count; }} | Where-Object { $_.Count -in 1 .. 3 }

 

Cheers,

Lain

Not sure why the error is there in your case, it does work on a 2022 DC with not that many groups but should work with a lot more groups without any problems. @LianRobertson posted an adjustment to your line below which should work
Yes that did the trick. Many thanks

worked fine thx.