I’m trying to run an LDAP query which will return all users which belong to the organisational units OU=Employees and OU=FormerEmployees and I am not getting anywhere.
I tried searching using the distinguishedName but that doesn’t appear to support wildcards. I know there has to be an easier way but my searching effort hasn’t yielded any results
If you’re on .NET 3.5 and newer, you can use a
PrincipalSearcherand a “query-by-example” principal to do your searching:If you haven’t already – absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in
System.DirectoryServices.AccountManagementIf you prefer the “old” .NET 2.0 style, you would need to create a base
DirectoryEntrythat corresponds to your OU you want to enumerate objects in, and then you need to create aDirectorySearcherthat searches for objects – something like this: