Get AD users in an OU tree

I thought I would run a series of posts showing the differences between working with the Microsoft AD cmdlets, the Quest AD cmdlets and ADSI scripts.  The idea is to compare and contrast the three methods. Usually the cmdlets are easier to work with but just occasionally being able to work from a first principles script can save some headaches.

This will be an occasional series that runs through the year

We’ll start with listing all of the users in an OU tree.

$ou = "OU=England,DC=Manticore,DC=org"            
            
"`nMicorosft"            
Get-ADUser -ResultSetSize 3000 -SearchBase $ou -SearchScope SubTree -Filter * |             
Format-Table Name, DistinguishedName            
            
"`nQuest"            
Get-QADUser -SizeLimit 3000 -SearchRoot $ou -SearchScope SubTree |             
Format-Table Name, DN            
            
            
"`nScript"            
            
$root = [ADSI]"LDAP://$ou"            
$search = [adsisearcher]$root            
$search.Filter = "(&(objectclass=user)(objectcategory=user))"            
$search.SizeLimit = 3000            
$results = $search.FindAll()            
            
foreach ($result in $results){            
    $result.Properties |             
    select @{N="Name"; E={$_.name}}, @{N="DistinguishedName"; E={$_.distinguishedname}}            
}


I’ve defined the OU.  In all cases I’ve set the result set to 3000 – notice the difference in parameter names



 



The Microsoft Get-ADUser and Quest cmdlets are very similar apart from some minor parameter names differences. Notice that he Microsoft cmdlet requires a –Filter parameter. * means everything



 



The script starts by creating a directory object for our search root using the [adsi] type accelerator. That is used to create a directorysearcher object the properties of which can be related to the cmdlets



The difference comes when we get the results – with the cmdlets we can just display (I’ve used format-table to ensure the script displays properly). With the script we have to iterate through the results and extract the relevant properties.



 



If there is a particular comparison you would like to see please let me know

Leave a Reply