Skip to content

Instantly share code, notes, and snippets.

@mortenya
Created February 6, 2015 21:49
Show Gist options
  • Save mortenya/c8e7bbfd78e65dacc059 to your computer and use it in GitHub Desktop.
Save mortenya/c8e7bbfd78e65dacc059 to your computer and use it in GitHub Desktop.
A very simple Directory Searcher example.
$strFilter = "(&(objectClass=Person)(objectCategory=User))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colProplist = "name", "samAccountName"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{$objItem = $objResult.Properties; $objItem.name}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment