Google Reviews
If you find this content or our services useful, please leave us a review.

Other Posts in this Series:

This post will examine the difference between ADSI and ADSISearcher when using PowerShell to query Active Directory.

ADSI and ADSISearcher are used to query Active Directory (AD) using Lightweight Directory Access Protocol (LDAP).

What is LDAP?

LDAP, as the name suggests, is a protocol that provides an interface to query AD directory services.  When we are querying AD we are returning one or more objects with unique identifiers (a Distinguished Name).  And we can do this using two type accelerators called ADSI and ADSISearcher.

What is a Type Accelerator?

A type accelerator is a simple alias to represent a .Net class.  We can return a list of type accelerators like so:

[System.Management.Automation.PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Get

You’ll notice that one of the type accelerators is psobject which references the class System.Management.Automation.PSObject. This means we could simplify our command above by using a type accelerator to get a list of all the type accelerators like so!

[psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Get

What is ADSISearcher?

[ADSISearcher] is a type accelerator for the class System.DirectoryServices.DirectorySearcher.  It is used to search for one or more objects based on a filter.

Consider this example, where we want to search all objects in AD and filter by user objects only:

$objSearcher=[adsisearcher]'(&(objectCategory=person)(objectClass=user))'

Or perhaps filter by only computers:

$objSearcher=[adsisearcher]'(&(objectCategory=computer))'

We can also get more complex by filtering for only enabled user objects in AD by adding a filter for the userAccountControl:

$objSearcher=[adsisearcher]'(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))'

And if we wanted to loop through all the enabled users we could do this:

$objSearcher=[adsisearcher]'(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))'

#specify page size to optimise caching memory required on DC
$objSearcher.PageSize = 200

#speed up the search by specifying only the properties we need
$colProplist = "distinguishedname"
foreach ($i in $colPropList) { $objSearcher.PropertiesToLoad.Add($i) | out-null } 

#use FindAll since we will potentially return many results
$colResults = $objSearcher.FindAll()

if ($colResults -ne $null) { 
    foreach($colResult in $colResults) {
        write-host ($colResult.Properties).distinguishedname
    }
}

We could also search for a single, specific user like so:

$objSearcher=[adsisearcher]'(&(objectCategory=person)(objectClass=user)(sAMAccountName=alkaneuser))'

And we could perform a simple search for this single user like so:

$objSearcher=[adsisearcher]'(&(objectCategory=person)(objectClass=user)(sAMAccountName=alkaneuser))'

#use FindOne since we're only returning a single result
$colResults = $objSearcher.FindOne()

if ($colResults -ne $null) { 
    write-host ($colResults.Properties).distinguishedname
}

All of the above searches are used to query the whole of AD.  But what if we wanted to only query a single object?  For example, an AD group?

What is ADSI?

[ADSI] is a type accelerator that represents the class System.DirectoryServices.DirectoryEntry.  It is used to bind directly to objects such as an AD group, an AD user or an AD computer.  It can then be used to update the attributes for a particular object.

In the above example, when we perform a search using FindOne() or FindAll() it returns one or many System.DirectoryServices.SearchResults.  We cannot modify the attributes of a search result – we use search results to simply print out attributes that we’re interested in.

If we wanted to update an attribute for an object returned as a search result, we need to create an ADSI object from this search result like so:

$objSearcher=[adsisearcher]'(&(objectCategory=person)(objectClass=user)(sAMAccountName=alkaneuser))'
$colResults = $objSearcher.FindOne()

if ($colResults -ne $null) { 
    
    #get path of the object from search result
    $pathToObject = ($colResults.Properties).adspath[0]

    #convert/cast search result object path to an ADSI object
    $user = [adsi]($pathToObject)

    #update the department
    $user.PutEx(1, "department", "alkane dept");

    #save it
    $user.SetInfo()
}

In the above example, we update the department attribute for a user called alkaneuser and set it to “alkane dept”.

The Difference Between ADSI and ADSISearcher
Comments have now been disabled. If you have a question to ask about this post please ask the community!