This article is aimed about how to get detailed information about Windows user accounts like accounts SID, SID type, Name, caption, Account domain and whether Account Status is OK, degraded unknown etc through WMI.
To access account information of windows users we need to Use System.Management name space.
To use this namespace first of all we need to add reference to System.management by
Project Menu>>Add Reference.
after that at the top of the code use
using System.Management; so that you can use of its classes .
Now its simple to get information about any Windows user Account through querying ManagementObjectSearcher Class .
we can get all information about Windows User through Win32_Account class .
so we need to query on that class to get related Management object and then we just need to get various properties related to it .
I have setup Interface like below that contain one combo box and some labels to get information.
private void Form1_Load(object sender, EventArgs e)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_Account");
foreach (ManagementObject mo in mos.Get())
{
cmbSelectUser.Items.Add(mo["Name"].ToString());
}
}
private void cmbSelectUser_SelectedIndexChanged(object sender, EventArgs e)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_Account where Name='" + cmbSelectUser.SelectedItem.ToString() + "'");
foreach (ManagementObject mo in mos.Get())
{
lblCaption.Text = mo["Caption"].ToString();
lblDescription.Text = mo["Description"].ToString();
lblDomain.Text = mo["Domain"].ToString();
lblLocalAccount.Text = ((bool)mo["LocalAccount"]).ToString();
lblName.Text = mo["Name"].ToString();
lblSID.Text = mo["SID"].ToString();
lblStatus.Text = mo["Status"].ToString();
lblSIDType.Text = mo["SIDType"].ToString();
}
}
Code is simple to understand just we have query the Management Object and displayed various properties .
Thank you :)