C#

Get Windows Users with C# and WMI

class SystemUser
{
	public enum AccountType
	{
		DUPLICATE_ACCOUNT = 256,
		NORMAL_ACCOUNT = 512,
		INTERDOMAIN_TRUST_ACCOUNT = 2048,
		WORKSTATION_TRUST_ACCOUNT = 4096,
		SERVER_TRUST_ACCOUNT = 8192
	};
 
	public AccountType Type { get; set; }
	public string Caption { get; set; }
	public string Description { get; set; }
	public bool Disabled { get; set; }
	public string Domain { get; set; }
	public string Fullname { get; set; }
	public bool LocalAccount { get; set; }
	public bool Lockout { get; set; }
	public string Name { get; set; }
	public bool PasswordChangeable { get; set; }
	public bool PasswordExpires { get; set; }
	public bool PasswordRequired { get; set; }
	public string SID { get; set; }
	public string Status { get; set; }
 
	public override string ToString()
	{
		return "Account Type: " + this.Type.ToString() + "\r\n" +
		"Caption: " + this.Caption + "\r\n" +
		"Description: " + this.Description + "\r\n" +
		"Disabled: " + (this.Disabled ? "YES" : "NO") + "\r\n" +
		"Domain: " + this.Domain + "\r\n" +
		"FullName: " + this.Fullname + "\r\n" +
		"Local Account: " + (this.LocalAccount ? "YES" : "NO") + "\r\n" +
		"Lockout: " + (this.Lockout ? "YES" : "NO") + "\r\n" +
		"Name: " + this.Name + "\r\n" +
		"Password Changeable: " + (this.PasswordChangeable ? "YES" : "NO") + "\r\n" +
		"Password Expires: " + (this.PasswordExpires ? "YES" : "NO") + "\r\n" +
		"Password Required: " + (this.PasswordRequired ? "YES" : "NO") + "\r\n" +
		"SID: " + this.SID + "\r\n" +
		"Status: " + this.Status;
	}
}
 
List<SystemUser> GetSystemUsers()
{
	string query = "SELECT * FROM Win32_UserAccount";
	List<SystemUser> users = new List<SystemUser>();
 
	using (ManagementObjectSearcher mObjectSearcher = new ManagementObjectSearcher(query))
	{
		foreach (ManagementObject mObject in mObjectSearcher.Get())
		{
			SystemUser su = new SystemUser();
 
			su.Type = (SystemUser.AccountType)Convert.ToInt32(mObject["AccountType"]);
			su.Caption = mObject["Caption"].ToString();
			su.Description = mObject["Description"].ToString();
			su.Disabled = Convert.ToBoolean(mObject["Disabled"]);
			su.Domain = mObject["Domain"].ToString();
			su.Fullname = mObject["FullName"].ToString();
			su.LocalAccount = Convert.ToBoolean(mObject["LocalAccount"]);
			su.Lockout = Convert.ToBoolean(mObject["Lockout"]);
			su.Name = mObject["Name"].ToString();
			su.PasswordChangeable = Convert.ToBoolean(mObject["PasswordChangeable"]);
			su.PasswordExpires = Convert.ToBoolean(mObject["PasswordExpires"]);
			su.PasswordRequired = Convert.ToBoolean(mObject["PasswordRequired"]);
			su.SID = mObject["SID"].ToString();
			su.Status = mObject["Status"].ToString();
 
			users.Add(su);
		}
	}
 
	return users;
}
 
//usage
foreach (SystemUser su in GetSystemUsers())
{
	Console.WriteLine("User\r\n" + su);
}