1

I know ManagementObjectSearcher and ManagementObjectCollection will be useful to get the details of windows user account. Is there any specific property that returns user account type (for ex: administrator,standard user,guest)?

Unknown
  • 73
  • 8
  • have u seen this https://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity%28v=vs.110%29.aspx ? – user786 Aug 24 '15 at 10:34
  • yes. I think we can only get the account type of the current user through windows identity. I need account type of all users. I don't know whether I fully understood windows identity or not. Can we use that to get all users account type? – Unknown Aug 24 '15 at 10:42

1 Answers1

0

Try this

  using System;
  using System.Management;
  using System.Linq;

 namespace ConsoleApplication5
{
class Program
{
    static void Main(string[] args)
    {
        ManagementObjectSearcher usersSearcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_UserAccount");
        ManagementObjectCollection users = usersSearcher.Get();

        var localUsers = users.Cast<ManagementObject>().Where(
            u => (bool)u["LocalAccount"] == true &&
                 (bool)u["Disabled"] == false &&
                 (bool)u["Lockout"] == false &&
                 int.Parse(u["SIDType"].ToString()) == 1 &&
                 u["Name"].ToString() != "HomeGroupUser$");

        foreach (ManagementObject user in users)
        {
            Console.WriteLine("Account Type: " + user["AccountType"].ToString());
            Console.WriteLine("Caption: " + user["Caption"].ToString());
            Console.WriteLine("Description: " + user["Description"].ToString());
            Console.WriteLine("Disabled: " + user["Disabled"].ToString());
            Console.WriteLine("Domain: " + user["Domain"].ToString());
            Console.WriteLine("Full Name: " + user["FullName"].ToString());
            Console.WriteLine("Local Account: " + user["LocalAccount"].ToString());
            Console.WriteLine("Lockout: " + user["Lockout"].ToString());
            Console.WriteLine("Name: " + user["Name"].ToString());
            Console.WriteLine("Password Changeable: " + user["PasswordChangeable"].ToString());
            Console.WriteLine("Password Expires: " + user["PasswordExpires"].ToString());
            Console.WriteLine("Password Required: " + user["PasswordRequired"].ToString());
            Console.WriteLine("SID: " + user["SID"].ToString());
            Console.WriteLine("SID Type: " + user["SIDType"].ToString());
            Console.WriteLine("Status: " + user["Status"].ToString());
        }

        Console.ReadKey();
    }
}
}

solution is from here How can I get a list Local Windows Users (Only the Users that appear in the windows Logon Screen)

Note: Framework 4.5 or above

Community
  • 1
  • 1
user786
  • 3,902
  • 4
  • 40
  • 72