locked
Retrieve Windows User Profiles RRS feed

  • Question

  • Does anyone know of a way to programatically retrieve a list of current Windows User Profiles, preferably with the associated user name and SID number.

    Thanks in advance.

    - Adam

    Thursday, December 14, 2006 5:45 PM

Answers

  • you could use WMI or you could also use this:

    System.Security.Principal.WindowsIdentity.GetCurrent().Owner;

    that gets the current user's SID

    http://msdn2.microsoft.com/en-us/library/system.security.principal.windowsidentity.getcurrent.aspx

    you can get the username also...

    System.Security.Principal.WindowsIdentity.GetCurrent().Name;

     

    in regards for other Windows User profiles you can do this using WMI. Add a reference to System.Management and import the System.Management and System.Management.Instrumentation namespaces, then you can do this:



    using System;
    using System.Management;
    using System.Windows.Forms;
    namespace UserSIDs
    {
        public class MyWMIQuery
        {
            public static void Main()
            {
                try
                {
                    ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher("root\\CIMV2",
                        "SELECT * FROM Win32_UserAccount");
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        MessageBox.Show("SID: " + queryObj["SID"].ToString());
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }
    }

     

    you can also get the username by adding this in the foreach loop...

    MessageBox.Show("Username: " + queryObj["Name"].ToString());

    Thursday, December 14, 2006 7:16 PM

All replies

  • you could use WMI or you could also use this:

    System.Security.Principal.WindowsIdentity.GetCurrent().Owner;

    that gets the current user's SID

    http://msdn2.microsoft.com/en-us/library/system.security.principal.windowsidentity.getcurrent.aspx

    you can get the username also...

    System.Security.Principal.WindowsIdentity.GetCurrent().Name;

     

    in regards for other Windows User profiles you can do this using WMI. Add a reference to System.Management and import the System.Management and System.Management.Instrumentation namespaces, then you can do this:



    using System;
    using System.Management;
    using System.Windows.Forms;
    namespace UserSIDs
    {
        public class MyWMIQuery
        {
            public static void Main()
            {
                try
                {
                    ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher("root\\CIMV2",
                        "SELECT * FROM Win32_UserAccount");
                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        MessageBox.Show("SID: " + queryObj["SID"].ToString());
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }
    }

     

    you can also get the username by adding this in the foreach loop...

    MessageBox.Show("Username: " + queryObj["Name"].ToString());

    Thursday, December 14, 2006 7:16 PM
  • Can you show me how to do the same using VB.net?
    Saturday, December 30, 2006 9:17 PM
  •  

    Imports System

    Imports System.Management

    Imports System.Windows.Forms

     

    Namespace UserSIDs

     

    Public Class MyWMIQuery

     

    Public Shared Sub Main()

    Try

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

    For Each queryObj As ManagementObject In searcher.Get()

    MessageBox.Show("SID: " & queryObj("SID").ToString())

    Next queryObj

    Catch e As ManagementException

    MessageBox.Show(e.Message)

    End Try

    End Sub

     

    End Class

     

    End Namespace

    Sunday, December 30, 2007 10:19 PM