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

Neil Hobson picture Neil Hobson · Oct 5, 2012 · Viewed 13.9k times · Source

How can I get a list of Local Windows Users (Only the Users that appear in the windows Logon Screen)

I have tried many methods using Windows Principle library & WMI Select commands. I keep getting Administrator, Guest & some other bizarre accounts (VUSRNEIL-DELL, $HOMEGROUPUSER, ASPNET...etc.)

Neither of these 3 user accounts appear on the Logon screen. How can I Differentiate between these user types?

I am coding in C#

Answer

Leniel Maccaferri picture Leniel Maccaferri · Oct 5, 2012

Just add a reference to System.Management in a Console Application and try this code:

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 localUsers)
            {
                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();
        }
    }
}