How to enumerate local profiles and profile paths
The main subroutine is the following:
vb Code:
Public LocalProfilePaths As List(Of String) = New List(Of String)
Public localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64)
Public RegistryPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
vb Code:
Sub GetLocalProfiles()
localKey = localKey.OpenSubKey(RegistryPath)
For Each subKeyName As String In localKey.GetSubKeyNames
Dim KeyToCheck = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64)
Dim newkey = RegistryPath & "\" & subKeyName
Try
Dim ProfilePath = KeyToCheck.OpenSubKey(newkey).GetValue("ProfileImagePath")
If Not (ProfilePath.IndexOf("windows", StringComparison.OrdinalIgnoreCase) >= 0) = True Then
LocalProfilePaths.Add(ProfilePath)
End If
Catch ex As Exception
End Try
Next
End Sub
This will enumerate through all the registry SIDs/profiles and get the profile path from the "ProfileImagePath"
Furthermore if the profileimagepath contains c:\windows\ or C:\Windows\ it will skip including it as these are system/service accounts.
You can also trim the path to get the username with something like this:
vb Code:
Try
For Each Profile As String In LocalProfilePaths
Dim ProfileName = Profile.Split(Path.DirectorySeparatorChar).GetValue((Profile.Split(Path.DirectorySeparatorChar).Length - 1)).ToString()
console.writeline("My profile name: " & ProfileName)
'Do stuff
Next
Catch ex As Exception
End Try