I am looking to return all the usernames and SIDs of users on a computer or network.
Has anyone got any idea how to do this?
I know I can return the SID of the current user using the code below:


Any help with this would be great!
I do not mind if the results are returned to a textbox or listview.

The easiest way to get system information is through Windows Management Instrumentation (WMI). There's a Win32_UserAccount class which provides information about user accounts including SID and Name properties which you asked for. I've been using WMI Code Creator utility to create "code skeletons" and then modified the code to match what I've needed. You can download WMI Code Creator from here. There are other WMI utilities too but I have found this one easy to use and it generates code for C#, VB.Net and VBScript.

Here's a sample code which gets user accounts' SID and name:

    Imports System
    Imports System.Management
    Imports System.Windows.Forms

    ' Code created with WMI Code Creator
    ' https://www.microsoft.com/en-us/download/details.aspx?id=8572

    Namespace WMISample

        Public Class MyWMIQuery

            Public Overloads Shared Function Main() As Integer

                    Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_UserAccount") 

                    For Each queryObj As ManagementObject in searcher.Get()

                        Console.WriteLine("Win32_UserAccount instance")
                        Console.WriteLine("Name: {0}", queryObj("Name"))
                        Console.WriteLine("SID: {0}", queryObj("SID"))
                Catch err As ManagementException
                    MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
                End Try
            End Function
        End Class
    End Namespace

If you are not familiar with WMI, search Microsoft's TechNet and MSDN for more information about WMI and how you can use it (and why you should use it).


This question has already been answered. Start a new discussion instead.