Searching for Groups

I do a lot of work with Active Directory.  One of the common tasks with AD is searching.  I saw a question about searching an OU for groups with "exec" in their name.  There are at least two approaches – a script and a cmdlet that can be used

Script

$root = [ADSI]"LDAP://OU=AllGroups,DC=starking,DC=org"

$search = [System.DirectoryServices.DirectorySearcher]$root
$search.Filter = "(&(objectcategory=group)(cn=*exec*))"
$result = $search.FindAll()

foreach ($group in $result)
{
  $group.properties.distinguishedname
}

This is a standard search.  Set the root of the search, set a filter and run findall().  The filter is looking for groups with "exec" in the name and is case insensitive.

Cmdlet

get-qadgroup -LdapFilter "(&(objectcategory=group)(cn=*exec*))" -SearchRoot "OU=AllGroups,DC=starking,DC=org"

The cmdlet is even easier to use and code.  Both give the same results.  If you can’t use the Quest cmdlets for any reason then the script is an alternative approach.

 

Technorati Tags: ,

This entry was posted in PowerShell and Active Directory. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s