And, sometimes you don't have a permission to access Active Directory. Its either in a different machine or you lack privileges or simply "They Dont like you in there".
To overcome the situation, it'd be wise to create a console application via C# or create a powershell script. I will be talking about how to do it developer way.
I won't lie. I came across the same problem and system administrators in the company was either too busy or didn't trust me having fooling around their server and Active Directory or they were probably scared I'd delete AD completely.
That's nonsense! I am an honest developer who simply wants to access their departments and report it.
That's nonsense! I am an honest developer who simply wants to access their departments and report it.
To Access a Directory, first of all, you need to add reference or import namespaces:
These 2 namespaces handle the job for you and give you the methods to access AD Users and their properties.
I'm going to give you the full source code I have developed to access a company's AD Department information and display it:
This code will display all the departments from AD and display all Distinct(Unique) departments.
The reason why we are trying to get distinct departments is, sometimes a department can be assigned twice or written against case sensitivity. So its wise to get once per department for healthy results.
I hope System Administrators don't dislike you because of accessing their AD without their knowledge.
- System.DirectoryServices
- System.DirectoryServices.AccountManagement
I'm going to give you the full source code I have developed to access a company's AD Department information and display it:
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.DirectoryServices;
- using System.DirectoryServices.AccountManagement;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ADUsers {
- class Program {
- public static List<object> deps = new List<object>();
- static void Main(string[] args) {
- using (var context = new PrincipalContext(ContextType.Domain, "iersoy.com")) {
- using (var searcher = new PrincipalSearcher(new UserPrincipal(context))) {
- foreach (var result in searcher.FindAll()) {
- DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
- Console.WriteLine(de.Properties[""].Value);
- }
- }
- }
- List<object> depsdis = deps.Distinct().ToList();
- foreach (var item in depsdis) {
- Console.WriteLine(item);
- }
- Console.ReadLine();
- }
- }
- }
The reason why we are trying to get distinct departments is, sometimes a department can be assigned twice or written against case sensitivity. So its wise to get once per department for healthy results.
I hope System Administrators don't dislike you because of accessing their AD without their knowledge.

Ankit BansalPosted Oct 1, 2015, 12:40 AM
Nice .. thanks for sharing..
Santhakumar MunuswamyPosted Sep 30, 2015, 3:32 PM
Good One
Shridhar SharmaPosted Sep 30, 2015, 12:33 PM
nice share sir
Nilesh JadavPosted Sep 30, 2015, 9:19 AM
Nice one sir
Humayun Kabir MamunPosted Sep 30, 2015, 4:55 AM
Nice...
Karthikeyan KPosted Sep 30, 2015, 4:09 AM
Good one...Thanks for sharing
RakeshPosted Sep 30, 2015, 3:30 AM
Nice share sir
Rajeesh MenothPosted Sep 30, 2015, 2:25 AM
Nice Share
Sibeesh VenuPosted Sep 30, 2015, 2:24 AM
Nice Share
Harshad PansuriyaPosted Sep 30, 2015, 1:53 AM
Nice One
Sujeet SumanPosted Sep 30, 2015, 1:15 AM
Nice Article..................