user1438082
user1438082

Reputation: 2748

Query only returns groups that have users

The code below returns the list of groups and associated members on a machine. Why does it only return populated groups. For example create a new user group on your machine and it will not be returned on this query. However if you add a user to the user group it will return in the query. Is there a fix to the query?

C# Code

var sGroupName = "";
var sUsername = "";
ManagementObjectSearcher searchresult = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_GroupUser");


foreach (ManagementObject queryObj in searchresult.Get())
{
         sGroupName = queryObj["GroupComponent"].ToString().Split(new[] { "Name=" }, StringSplitOptions.None).Last().Trim('"');
         sUsername = queryObj["PartComponent"].ToString().Split(new[] { "Name=" }, StringSplitOptions.None).Last().Trim('"');
}

Upvotes: 0

Views: 76

Answers (1)

Rufus L
Rufus L

Reputation: 37060

Try this:

var searchResult = 
    new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Group");

Upvotes: 1

Related Questions