Finding Groups with the Active Directory Searcher and PowerShell

Posted on May 22, 2017 by Jeff Hicks in Active Directory, PowerShell with

We will continue looking at ways to use the Active Directory Searcher with PowerShell. If you are just joining in, you might want to start at the beginning with this article. If you have been following along, you know that this is a powerful way to work with Active Directory. It works without requiring the Active Directory module that is a part of Remote Server Administration Tools (RSAT). Let’s start with a fresh searcher object on a domain member that is configured with a filter to find group objects.

 

 

We will leave the search root alone, which will default to the domain root. Let’s see what type of object we can find.

 

Getting an AD group object (Image Credit: Jeff Hicks)

Getting an AD Group Object (Image Credit: Jeff Hicks)

 

Even though this is a group, it is still an Active Directory search result. This means that I can use techniques from my previous articles like specifying the properties to retrieve and Convert-ADSearchResult function.

 

Now, I can get all the groups in my domain.

A sample group (Image Credit: Jeff Hicks)

A Sample Group (Image Credit: Jeff Hicks)

 

I can look at $groups by itself. I can also pipe it to Out-Gridview or Export-Csv. Really, I can do just about anything I want with it.

There is one bit of information that is not readily apparent in the output. It is difficult to tell what is a security or distribution group and whether it is global or universal. This information is available but it is hidden in the GroupType property.  When you search for a group, you should get a result with a GroupType property that matches one of these values:

Value Scope Category
2 Global Distribution
8 Universal Distribution
-2147483640 Universal Security
-2147483643 DomainLocal Security
-2147483644 DomainLocal Security
-2147483646 Global Security

 

The -2147483643 value is for built-in groups and -2147483644 is for default groups found in the User container. Of course, you can filter for any of the group types.

Filtered groups by type (Image Credit: Jeff Hicks)

Filtered Groups by Type (Image Credit: Jeff Hicks)

 

What would make this even easier? Let’s look at a function to get group objects.

 

This relies on my conversion function to properly format the results. The function properly decodes the GroupType property and adds 2 new properties, GroupScope and GroupCategory. It also adds a property that shows the total member count. Now, I have an easy way to find a group.

 

Testing the AD group function (Image Credit: Jeff Hicks)

Testing the Active Directory Group Function (Image Credit: Jeff Hicks)

 

I can also limit the search to a particular organizational unit. Just like with any other PowerShell command, I can use the output.

Getting AD groups by member count (Image Credit: Jeff Hicks)

Getting Active Directory Groups by Member Count (Image Credit: Jeff Hicks)

 

Sponsored

You could easily extend the function to add other filtering options, such as group category, scope, or test for empty groups. I hope you will grab a copy and try it out in your test environment. Next time, we will look at how to use the Active Directory Searcher with thousands of results. Feel free to leave a comment about what you think of this little series.

Tagged with , , , ,

Register for this Webinar

How Replication Supports Your Company’s RTOs & RPOs
Join us for this free webinar

Can you have your workloads running within the agreed RTOs? Join this webinar with expert speakers from Veeam to exceed business objectives with an RPTO<15 min for ALL of your application and data.

Thursday, December 14, 2017 at 11 a.m EST