Skip to content

Instantly share code, notes, and snippets.

@rcknight
Created October 17, 2013 09:09
Show Gist options
  • Save rcknight/7021669 to your computer and use it in GitHub Desktop.
Save rcknight/7021669 to your computer and use it in GitHub Desktop.
Query all active ad users who are real people (has a job title)
using (var context = new PrincipalContext(ContextType.Domain, "PHARMAXO.local", "OU=Corsham,dc=pharmaxo,dc=local"))
{
var qbeUser = new UserPrincipal(context) { Enabled = true };
using (var searcher = new PrincipalSearcher(qbeUser))
{
var results = searcher.FindAll().Select(r => r.GetUnderlyingObject() as DirectoryEntry);
var names = results.Where(r => r.Properties["title"].Value != null && ((string)r.Properties["title"].Value) != "")
.Select(r => String.Format("{0} {1}", r.Properties["givenName"].Value, r.Properties["sn"].Value))
.OrderBy(r => r);
foreach(var name in names)
Console.WriteLine (name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment