0

Is there a better way to be able to count the amounts of empty groups than using this method and then calling $count + 1 for each result? I tried to mess around with $123.count but it kept returning 0

$Groups = Get-ADGroup -Properties * -Filter * | where { $_.Members.Count -eq 0}
$123= Foreach($G In $Groups)
{
    $Membership = Get-ADGroupMember -Identity $G.Name
    If($Membership.count -eq 0){
    $Count = $count + 1
    }

}
4
  • 1
    @($Groups).Count Commented Aug 17, 2016 at 13:31
  • @MathiasR.Jessen That would only give empty groups, but the groups can still be members of something. I want $groups to have gone through $Membership.count -eq 0 Commented Aug 17, 2016 at 13:33
  • So you want to count the number of groups that have no members and are themselves not member of any groups? Commented Aug 17, 2016 at 13:37
  • @MathiasR.Jessen Correct. I also would like to not use "$count +1" Commented Aug 17, 2016 at 13:40

1 Answer 1

3

Get-ADGroup can do much of what you ask without going away from AD.

Getting this done with the Filter parameter is painful. The LDAP filter however is simple.

For empty groups:

Get-ADGroup -LdapFilter "(!member=*)"

For empty groups which are also not nested inside other groups:

Get-ADGroup -LdapFilter "(&(!memberOf=*)(!member=*))"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.