Getting a list of user profile paths on a computer

Darren Rose 281 Reputation points
2020-12-02T14:48:58.73+00:00

Hi

I am trying to obtain a list of all user profile paths on a machine - just those which are real users so excluding ones like default, public, network services etc

I can do it successfully by iterating HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList as per code below

Just wondered it anyone has a better method, or if there is some built in .NET command / function to do this

Thanks

Public Class Form1

    Dim paths As New List(Of String)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim registrypath As Microsoft.Win32.RegistryKey = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry64)

        Using key = registrypath.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")

            For Each subKeyName In key.GetSubKeyNames()
                Using subKey = key.OpenSubKey(subKeyName, True) ' true so opened as writable if not get UnauthorizedAccessException below

                    If subKey.GetValue("FullProfile") IsNot Nothing Then

                        If subKey.GetValue("FullProfile").Equals(1) Then

                            paths.Add(subKey.GetValue("ProfileImagePath"))

                        End If

                    End If

                End Using
            Next
        End Using

        Stop

    End Sub

End Class
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,737 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 85,881 Reputation points
    2020-12-02T15:21:43.02+00:00

    One of the ways is with WMI (Win32_UserAccount)
    that you can filter for example from Disabled field

    0 comments No comments

  2. RLWA32 45,701 Reputation points
    2020-12-02T15:50:02.49+00:00

    And also from WMI Win32_UserProfile class

    0 comments No comments

  3. Darren Rose 281 Reputation points
    2020-12-02T19:34:58.117+00:00

    Thank you both that is handy to know, not sure if WMI is better than what I am currently doing though


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.