Skip to content

Powershell-ized adfind to count objects

10/10/2012

In trying to count objects in a relatively large active directory (well over 1 million objects) Quest’s cmdlets are too slow, and I kept getting a weird memory leak with System.DirectoryServices. So why not try the old faithful adfind, by Joe Richards, just powershell-ized a little.

(((adfind.exe -c -b <searchroot> -f <filter> 2>&1)[-1]).split(" "))[0]

returns a single number for the count of the objects, and is pretty quick, plus saves about 6 lines of code.

e.g. all security groups:

# get object count

$count = [int](((adfind.exe -c -b "dc=example,dc=com" -f "(&(objectclass=group)(|(samaccounttype=536870912)(samaccounttype=268435456)))" 2>&1)[-1]).split(" "))[0]

# NB it outputs a string so you need the [int] cast to do any arithmetic on it
Advertisements
Leave a Comment

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: