Joe Pickering

I am looking for help. I have a VBScript that I have been using to check the local groups on PC's to make sure no one is adding users to groups. I want to rewrite it in C# now but I can't find what to call or how to call it. Any help or direction would be appreciated.

Here is the code I use in VB

strComputer = "ntcpc-u00p340"
Set colGroups = GetObject("WinNT://" & strComputer & "")
colGroups.Filter = Array("group")
For Each objGroup In colGroups
  Wscript.Echo objGroup.Name 
  For Each objUser in objGroup.Members
    Wscript.Echo vbTab & objUser.Name

Re: Visual C# General VBScript to C# .NET Converstion

Sergio Arizmendi - exMSFT

Altho it can be done with DirectoryServices, my expertise is WMI. So, here it is. You must add a reference to System.Management for this to work. I hope it solves your problem:

using System;

using System.Collections.Generic;

using System.Text;

using System.Management;

namespace ConsoleApplication1


class Program


static void Main(string[] args)


string strComputer = "SERGIODUOV";

// using WMI

ManagementObjectSearcher groupSearcher = new ManagementObjectSearcher(@"\\" + strComputer + @"\root\cimv2", "SELECT * FROM Win32_Group WHERE LocalAccount = TRUE");

foreach (ManagementObject groupObj in groupSearcher.Get()) {

Console.WriteLine("Group Name: {0}", groupObj["Name"].ToString());

ManagementObjectSearcher userSearcher = new ManagementObjectSearcher(@"\\" + strComputer + @"\root\cimv2", "SELECT * FROM Win32_GroupUser WHERE GroupComponent='Win32_Group.Name=\"" + groupObj["Name"].ToString() + "\",Domain=\"" + groupObj["Domain"].ToString() + "\"'");

foreach (ManagementObject groupuserObj in userSearcher.Get()) {

ManagementObject userObj = new ManagementObject(groupuserObj["PartComponent"].ToString());

Console.WriteLine("User Name: {0} ", userObj["Name"].ToString());






Regards :)