Get users from local user groups and active directory groups

Get users from local user groups and active directory groups
4 votes, 4.75 avg. rating (93% score)


In this post we will see how to get users from local user groups and from active directory group.
What are the points that are covered

  • How to get list of users from local user groups
  • How to navigate users in active directory

Get list of users from local user groups

The following method returns arraylist object that contains users in local user group

		using System.DirectoryServices;
		private ArrayList GetUserId(string SystemUserGroup)
            string userId = "";
            ArrayList userIds = new ArrayList();
            string directoryString = "WinNT://./" + SystemUserGroup + ",group";
            using (DirectoryEntry groupEntry = new DirectoryEntry(directoryString))
                foreach (object member in (IEnumerable)groupEntry.Invoke("Members"))
                    using (DirectoryEntry memberEntry = new DirectoryEntry(member))
                        string userIdValue = memberEntry.Path;
                        //output will be WinNT://system1/user1
                        //split with '//' to the user code
                        string[] user = userIdValue.Split(new char[] { '\\' });
                        userId = user[user.Length - 1];

            return userIds;

Get list of users from active directory

The following code shows how we can loop active directory

			using System.DirectoryServices.AccountManagement;
			string groupName = "Administrators";
            string domainName = "";

            PrincipalContext prCtx = new PrincipalContext(ContextType.Domain, domainName);
            GroupPrincipal grPrincipal = GroupPrincipal.FindByIdentity(prCtx, IdentityType.SamAccountName, groupName);

            if (prCtx != null)
                foreach (Principal prnc in prCtx.GetMembers(false))
                    string AccountName = prnc.SamAccountName;
                    string DisplayName = prnc.DisplayName;

Other References

If you are using .NET Framework 2.0 some useful links how to navigate users in the active directory


Any task to access active directory or local user groups, hope this post gives you quick start.

August 2, 2012 В· Adi В· No Comments
Tags: ,  В· Posted in: C#, Sharepoint 2007, Sharepoint 2010

Leave a Reply

What is 12 + 12 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)