Avatar of Elliott Ward
Elliott Ward
 asked on

How can I get a list of AD groups that match a partial name?

I need to search AD for any groups that match a partial name, then search each group for the users inside it.

I have the code to list the users in a group, listed below.  But how do I get the listing of the groups to search?

Public Function UsersInActiveDirectoryGroup(ByVal GroupName As String) As Specialized.StringCollection
   Dim members As New Specialized.StringCollection

       Dim domainContext As New ActiveDirectory.DirectoryContext(ActiveDirectory.DirectoryContextType.Domain)
       Using dc As ActiveDirectory.DomainController = ActiveDirectory.DomainController.FindOne(domainContext)
           Using pctx As New PrincipalContext(ContextType.Domain, dc.Domain.Name)
                Using grp As GroupPrincipal = GroupPrincipal.FindByIdentity(pctx, IdentityType.Name, GroupName)
                    If Not IsNothing(grp) Then
                        For Each p As Principal In grp.GetMembers(True)
                     End If
                 End Using
             End Using
         End Using
         Return members
     Catch ex As Exception
         Return Nothing
     End Try
 End Function
Active DirectoryVisual Basic.NET

Avatar of undefined
Last Comment
Elliott Ward

8/22/2022 - Mon
Nasir Razzaq

Here is a comprehensive tutorial. See if it helps


I'm not too familiar with this kind of code in general, but the main thing you would add is:


where "BLAH" is the name you want it to match.

Dont know how big of a userbase you got . but i normally use the manage engine ad manager plus to get all kinds of reports . They also have a free version for 100 users or so ..
ADManager Plus- Active Directory Management & Reporting
Ad Manager Plus
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Elliott Ward

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Elliott Ward

Thanks for your help.