• Here's a slightly different approach: start with the the matching groups, so you don't process users that aren't members of any of those groups.

    Then count the groups for each of the distinct members.

    I'm sure there is an infinitely faster way of counting the groups per user than what I have below...

    cls

    ## Get the groups

    $strFilter = "(&(objectCategory=group))"

    $objDomain = New-Object System.DirectoryServices.DirectoryEntry

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher

    $objSearcher.SearchRoot = $objDomain

    $objSearcher.PageSize = 2500 # Default is 1000. If you have less than 1000 groups, you can omit this line

    $objSearcher.Filter = $strFilter

    $Results = @()

    $colProplist = "cn", "member"

    foreach ($i in $colPropList)

    {

    $null = $objSearcher.PropertiesToLoad.Add($i)

    }

    #Write-date "Search - Groups"

    $colResults = $objSearcher.FindAll()

    foreach ($objResult in $colResults)

    {

    $objItemName = $objResult.Properties.cn

    $objItemPath = $objResult.Path

    [string]$strMember = $objResult.Properties.member

    [string]$strName = $objItemName

    if (($strName -like "GRP1_*"))

    {

    $a = $strMember -split "CN="

    foreach ($item in $a)

    {

    $item0 = $item -split ","

    $strUser = $item0[0]

    if ($strUser -ne "")

    {

    $Results += ("$strName,$strUser")

    }

    }

    }

    }

    # This is the slow part:

    $Users = @()

    $Users += $Results | %{$_ -split ","[1]}

    $Users = $Users | Sort-Object -Unique

    foreach ($User in $Users)

    {

    $Count = ($Results | ? {$_ -match $User}).Count

    if (!($Count)) {$Count = 1}

    Write-Host $User"; "$Count

    }