this code runs well. It was copied from Microsoft.
$strFilter = "(&(objectCategory=person)(objectClass=user))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$colProplist ="Name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{$objItem = $objResult.Properties; $objItem.name }
However, I did not understand why the teacher have used
$strFilter = "(&(objectCategory=person)(objectClass=user))"
because all information I have got from web were this symbol & is for execute a code, once its meaning is "Call Operator".
I think that, in this case, it has an other meaning.
(&(objectCategory=person)(objectClass=user))
is equivalent to the following conditional in programming language context:
objectCategory == person && objectClass == user
It is documented on MSDN page for DirectorySearcher.Filter Property
:
Compound expressions are formed with the prefix operators
&
and|
. An example is "(&(objectClass=user)(lastName= Davis))
". Another example is "(&(objectClass=printer)(|(building=42)(building=43)))
".
So, the filter is applied as:
objectClass=user && lastName= Davis
and
objectClass=printer && ( building=42 || building=43)
respectively, in the snippet provided.