PowerShell Script to Get All Users from LDAP

Wanted to retrieve all the users from a AD LDS based LDAP instance. Code

$Dom = "LDAP://<Server-Name>/CN=Users,CN=LDAP,DC=SharePoint,DC=COM"
$Root = New-Object DirectoryServices.DirectoryEntry $Dom

# Create a selector and start searching from the Root of AD
$selector = New-Object DirectoryServices.DirectorySearcher
$selector.SearchRoot = $root
# Filter the users with -like "CN=Person*". Note the ForEach loop
$adobj= $selector.findall() | where {
	$_.properties.objectcategory -like "CN=Person*"
}
foreach ($person in $adobj)
{
	$prop=$person.properties
	Write-host "First name: $($prop.givenname) Surname: $($prop.sn) User: $($prop.cn)"
}
Write-host "There are $($adobj.count) users in the $($root.name) domain" 

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *