Listing/Modifying Server Information with Visual Basic.NET and VBScript Using WMI - How to retrieve all user account information using WMI
(Page 3 of 5 )
How can we list all the user accounts on your server? The following VB.NET code would achieve this result.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_UserAccount")
Dim dt As DataTable = globals.getUserAccountStructure
()
Dim en As ManagementObjectEnumerator =
searcher.Get.GetEnumerator
While en.MoveNext
Dim queryObj As ManagementObject = en.Current
globals.addUserAccountRow(dt, queryObj
("FullName"), queryObj("Name"), queryObj("Domain"), queryObj
("LocalAccount"))
End While
dt.AcceptChanges()
Me.DataGrid1.DataSource = dt
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
End Sub
I excluded some of the properties (because it would make the program too long) from the existing WMI class (Win32_UserAccount) to give you only the most important ones. The above program would list the full name, user name, domain the user belongs to and whether or not it is a local account. You can further extend the above program with several other properties available in the “Win32_UserAccount” class. You can even refer to MSDN online for further properties.
You can achieve the same result with VBScript by using the following code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_UserAccount",,48)
For Each objItem in colItems
Wscript.Echo "FullName: " & objItem.FullName
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "Domain: " & objItem.Domain
Wscript.Echo "LocalAccount: " & objItem.LocalAccount
Next
Next: How to retrieve all group information along with account information using WMI >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee