Saturday, October 16, 2010

PowerShell ADSISEARCHER Basics

The ActiveDirectory class of the Net classes has a ‘type accelerator’ in PowerShell; [adsisearcher]. This is shorthand for [System.DirectoryServices.DirectorySearcher]

We can create an instance of this object like this:

$searcher=[adsisearcher]””

This can then be used exactly as if we used the full class name:

$searcher=[System.DirectoryServices.DirectorySearcher]””

Which is the same as:

$searcher=New-Object System.DirectoryServices.DirectorySearcher(””)

Here are some of the easy ways we can use this object:

#find all objects with a name that starts with ‘d’,
([adsisearcher]"name=d*").FindAll()

#find an accoun with samAcountName that starts with ‘d’
([adsisearcher]"samAccountName=d*").FindAll()

#find all objects of class ‘user’
([adsisearcher]"objectCategory=user").FindAll()

#find all groups
([adsisearcher]"objectClass=group").FindAll()

Note the use of wildcards.

These are just some of the simple uses for this object. We can filter on an property name, class and category. With LDAP filter syntax we can AND, OR, NOT any amount of complexity into our query.

http://msdn.microsoft.com/en-us/library/ms675768(VS.85).aspx

No comments:

Post a Comment