none
Get List of Users and "Member Of" list on local machine in C# RRS feed

  • Question

  • Hello, how can I get the info you see in the Computer Management Console under Local Users and Groups.

    I need to be able to see a list of users and for each user the "member of" list as you would see in on this properties tab?

    I would to catch if someone setup "HomeUsers" for the main account and T4CAdmin account as this causes many permission issues with our software. So if a tool is able to detect this, it would speed up troubleshooting as well as getting them to setup the PC correctly from the start.

    Thursday, March 7, 2019 7:40 PM

Answers

  • For example, a way to get the list of users + their groups in C# :

    string sPath = "WinNT://" + Environment.MachineName + ",computer";
    using (var computerEntry = new DirectoryEntry(sPath))
        foreach (DirectoryEntry childEntry in computerEntry.Children)
            if (childEntry.SchemaClassName == "User")
            {
                Console.WriteLine(childEntry.Name);
                object obGroups = childEntry.Invoke("Groups");
                foreach (object ob in (System.Collections.IEnumerable)obGroups)
                {                              
                    DirectoryEntry obGpEntry = new DirectoryEntry(ob);
                    Console.WriteLine("\t" + obGpEntry.Name);
                    obGpEntry.Close();
                }         
            }



    • Edited by Castorix31 Friday, March 8, 2019 9:06 AM
    • Marked as answer by Domleg Wednesday, March 13, 2019 7:56 PM
    Friday, March 8, 2019 8:49 AM

All replies

  • It is done with Active Directory Service (IADsUser::Groups)

    (System.DirectoryServices in .NET)

    Thursday, March 7, 2019 8:30 PM
  • Can you provide an example?
    Thursday, March 7, 2019 10:07 PM
  • For example, a way to get the list of users + their groups in C# :

    string sPath = "WinNT://" + Environment.MachineName + ",computer";
    using (var computerEntry = new DirectoryEntry(sPath))
        foreach (DirectoryEntry childEntry in computerEntry.Children)
            if (childEntry.SchemaClassName == "User")
            {
                Console.WriteLine(childEntry.Name);
                object obGroups = childEntry.Invoke("Groups");
                foreach (object ob in (System.Collections.IEnumerable)obGroups)
                {                              
                    DirectoryEntry obGpEntry = new DirectoryEntry(ob);
                    Console.WriteLine("\t" + obGpEntry.Name);
                    obGpEntry.Close();
                }         
            }



    • Edited by Castorix31 Friday, March 8, 2019 9:06 AM
    • Marked as answer by Domleg Wednesday, March 13, 2019 7:56 PM
    Friday, March 8, 2019 8:49 AM
  • Hi Domleg,

    Here is a example about how to get the memberOf property of current user.

      public static void GetAD()
            {
                string Name = Environment.GetEnvironmentVariable("USERNAME");           
                using (var context = new PrincipalContext(ContextType.Domain))
                {
                    using (UserPrincipal user = new UserPrincipal(context))
                    {
                        user.SamAccountName = Name;
                        using (var searcher = new PrincipalSearcher(user))
                        {
                            foreach (var result in searcher.FindAll())
                            {
                                DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
                                var groupName = de.Properties["memberOf"].Value;
                             }
                        }
                    }
    
                }
                Console.ReadLine();
            }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 11, 2019 5:29 AM
    Moderator