The below code gets me the users in the group but it is returned
"CN=johnson\,Tom,OU=Users,OU=Main,DC=company,DC=com"
I want to just return the First and Last name. How can I accomplish this?
DirectoryEntry ou = new DirectoryEntry();
DirectorySearcher src = new DirectorySearcher();
src.Filter = ("(&(objectClass=group)(CN=Gname))");
SearchResult res = src.FindOne();
if (res != null)
{
DirectoryEntry deGroup = new DirectoryEntry(res.Path);
PropertyCollection pcoll = deGroup.Properties;
foreach (object obj in deGroup.Properties["member"])
{
ListBox1.Items.Add(obj.ToString());
}
}
This is a PowerShell script that I made to do it without using the AccountManagement classes. It should be easy enough to translate it to C#:
Using your code, the givenName (first name) and sn (last name) properties should work.
If you use the System.DIrectoryServices.AccountManagement namespace UserPrincipal (as @russell-mcclure suggested), you will find GivenName and Surname properties also.
AccountManagement is very handy unless you have to traverse a trusted forest and need the global catalog to find the user.
I prefer using the classes in System.DirectoryServices.AccountManagement:
Search through the group.Members property until you have a Principal that you want. Then extract the name like this: