Getting Objects Out of AD


 This is dsquery.exe computer -name <foo>

function Get-ComputerObjects { 
param( [string]$name="*" );

 $directorySearcher = new-object DirectoryServices.DirectorySearcher([ADSI]"LDAP://$env:userDnsDomain"); 
$directorySearcher.PageSize = 1000;
$directorySearcher.Filter = "(&(objectCategory=computer)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(name=$name))";
$directorySearcher.FindAll()
}

 


Comments (1)

  1. Jamie Akers says:

    This example is fine when the number of objects is under 1000.  However you probably want to implement a wrapper method when working with larger results sets.  There's a good article with some sample code here:

    stackoverflow.com/…/can-i-get-more-than-1000-records-from-a-directorysearcher-in-asp-net

    I've run this code and can confirm it does a very elegant job.  It also gives greater control of memory usage during what could be a large search operation.