Searching an entire Active Directory forest for certain objects…

In this blog entry we explore how to find all group objects in an Active Directory forest with a name that contains “XYZ”.

Good evening everyone! After several weeks of dodging postings of an applied nature I figured that it might be time to do a PowerShell focused posting. I like PowerShell…

Anyhow, I recently was doing something at a client site and I wanted to find groups that contained a certain string pattern in their name. If I were searching for groups in a single domain, this would be a very simple chore. However, I wanted to find groups that matched the pattern in all domains within the forest. Technically, I could search the entire forest using the [ADSI] type adapter. However, to go get the objects from all the domains in the forest would have required me to create a DirectorySearcher object for each domain, and then search that domain.

In other words, doable… but more code then I wanted to type. So… I instead decided to use the Quest AD cmdlets and came up with the following:

$Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$Domain.Forest.Domains | foreach {Get-QADObject -Service "$($" -LdapFilter "(&(objectClass=group)(cn=*Pattern123*))"} | ft Name,Description

Following the two lines of code, I first create a $Domain object using the GetCurrentDomain method of the DirectoryServices.ActiveDirectory.Domain class. Then using this $Domain object, I get a list of all of the domains in the forest. Next, I loop through that list of domain and search for the groups matching my defined pattern using the Get-QADObject.


If you like this, check out some other posts from Tyson:

Or if you want, you can also check out some of Tyson's latest publications:

Lastly, visit the Microsoft Subnet for more news, blogs, and opinions from around the Internet. Or, sign up for the bi-weekly Microsoft newsletter. (Click on News/Microsoft News Alert)

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2010 IDG Communications, Inc.

SD-WAN buyers guide: Key questions to ask vendors (and yourself)