48

Is there a way to connect between the values under HKEY_USERS to the actual username?
I saw some similar questions, but most (if not all) talks about C# code, and my need is in VBScript.

modz0r
  • 1,022
  • 1
  • 10
  • 17

9 Answers9

73

If you look at either of the following keys:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\hivelist

You can find a list of the SIDs there with various values, including where their "home paths" which includes their usernames.

I'm not sure how dependable this is and I wouldn't recommend messing about with this unless you're really sure what you're doing.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • 12
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\hivelist seems to only be the currently logged in user on every system (Xp - 7) I have looked at... And I would not consider HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\\ProfileImagePath a reliable way of determining username (See http://support.microsoft.com/kb/2454362). – user66001 Nov 23 '12 at 21:51
  • `reg query 'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'` Find the corresponding SID in the list you are interested in, then run the following replacing SID with the actual SID string. `reg query 'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\SID'` ProfileImagePath contains the home directory with username – DGM Mar 29 '21 at 16:14
35

It is possible to query this information from WMI. The following command will output a table with a row for every user along with the SID for each user.

wmic useraccount get name,sid

You can also export this information to CSV:

wmic useraccount get name,sid /format:csv > output.csv

I have used this on Vista and 7. For more information see WMIC - Take Command-line Control over WMI.

dcharles
  • 4,822
  • 2
  • 32
  • 29
  • Unfortunately, the SIDs I get don't match up with values under HKEY_USERS on Windows 7 x64. – Zian Choy May 24 '14 at 04:16
  • It's possible for a legit SID to be seemingly absent because that particular user's hive is not loaded right at that moment. `Runas /profile /user:desireduser somearbitrarycommand` will load it. – amonroejj Oct 13 '16 at 12:55
  • I don't know if this is accessible from VBScript but this is the best answer if you just want to get the list "manually" – ssimm Feb 02 '20 at 15:31
  • only non-domain users? – Hicsy Mar 24 '20 at 22:18
16
  1. Open Reg HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\

  2. make a loop to get all subkeys

  3. the subkeys you are interested with are those started with [S-1-5-21-] which means user (see key name [ProfileImagePath] they are always started with a path c:\Users)

  4. Those starting with [S-1-5-21-12] are all local users

  5. Those starting with [S-1-5-21-13] are all network users [if joined to Domained network] that are previously logged on the machine.

Stone
  • 191
  • 1
  • 2
  • 5
    3 contains an error, and not wise advise. [ProfileImagePath] doesn't HAVE to start with a path c:\Users... Look at https://www.google.com/#hl=en&tbo=d&output=search&sclient=psy-ab&q=move+windows+7+user+directory; Also, as elsewhere commented on this thread, and mentioned by @spade if the user / administrator has changed the users username post account creation and login, the profile directory name will no longer match the username (http://support.microsoft.com)/kb/2454362) – user66001 Nov 23 '12 at 21:59
8

By searching for my userid in the registry, I found

HKEY_CURRENT_USER\Volatile Environment\Username
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Alan Frank
  • 89
  • 1
  • 2
3

You can use the command PSGetSid from Microsoft's SysInternals team.

Download URL: http://technet.microsoft.com/en-gb/sysinternals/bb897417.aspx

Usage:

psgetsid [\\computer[,computer[,...] | @file] [-u username [-p password]]] [account|SID]
-u  Specifies optional user name for login to remote computer.
-p  Specifies optional password for user name. If you omit this you will be prompted to enter a hidden password.
Account PsGetSid will report the SID for the specified user account rather than the computer.
SID PsGetSid will report the account for the specified SID.
Computer    Direct PsGetSid to perform the command on the remote computer or computers specified. If you omit the computer name PsGetSid runs the command on the local system, and if you specify a wildcard (\\*), PsGetSid runs the command on all computers in the current domain.
@file   PsGetSid will execute the command on each of the computers listed in the file.

Example:

psgetsid S-1-5-21-583907252-682003330-839522115-63941

NB:

  • Where the user is a domain/AD(LDAP) user, running this on any computer on the domain should give the same results.
  • Where the user is local to the machine the command should either be run on that machine, or you should specify the computer via the optional parameter.

Update

If you use PowerShell, the following may be useful for resolving any AD users listed:

#create a drive for HKEY USERS:
New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS -ErrorAction SilentlyContinue

#List all immediate subfolders
#where they're a folder (not a key)
#and they's an SID (i.e. exclude .DEFAULT and SID_Classes entries)
#return the SID
#and return the related AD entry (should one exist).
Get-ChildItem -Path 'HKU:\' `
| ?{($_.PSIsContainer -eq $true) `
-and ($_.PSChildName -match '^S-[\d-]+$')} `
| select @{N='SID';E={$_.PSChildName}} `
, @{N='Name';E={Get-ADUser $_.PSChildName | select -expand Name}}

You could also refine the SID filter further to only pull back those SIDs which will resolve to an AD account if you wished; more on the SID structure here: https://technet.microsoft.com/en-us/library/cc962011.aspx

JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
2

In the HKEY_USERS\oneyouwanttoknow\ you can look at \Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders and it will reveal their profile paths. c:\users\whothisis\Desktop, etc.

Kingsley
  • 14,398
  • 5
  • 31
  • 53
  • HKEY_USERS only has the actual logged in users and the default users like "NT AUTHORITY\SYSTEM" – f4d0 May 06 '19 at 11:14
1
for /f "tokens=8 delims=\" %a in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\hivelist" ^| find "UsrClass.dat"') do echo %a
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
DaLua
  • 19
  • 1
1

The proper way to do this requires leveraging the SAM registry hive (on Windows 10, this requires NT AUTHORITY\SYSTEM privileges). The information you require is in the the key: HKEY_LOCAL_MACHINE\SAM\SAM\Domains\Account\Users\Names.

Each subkey is the username, and the default value in each subkey is a binary integer. This value (converted to decimal) actually corresponds to the last chunk of the of the SID.

Take "Administrator" for example, by default it is associated with the integer 0x1f4 (or 500).

So, in theory you could build a list of SIDS based on the subkey names of the HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList key and/or HKEY_USERS key, parse out the the value after the last hyphen (-), and compare that to the info from the SAM hive.

If you don't have NT AUTHORITY\SYSTEM privileges, the next best way to approach this may be to follow the other method described in the answers here.

reference: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/81d92bba-d22b-4a8c-908a-554ab29148ab

SW_user2953243
  • 334
  • 1
  • 12
1

Done it, by a bit of creative programming,

  1. Enum the Keys in HKEY_USERS for those funny number keys...

  2. Enum the keys in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\

and you will find the same numbers.... Now in those keys look at the String value: ProfileImagePath = "SomeValue" where the values are either:

"%systemroot%\system32\config\systemprofile"... not interested in this one... as its not a directory path...

%SystemDrive%\Documents and Settings\LocalService - "Local Services" %SystemDrive%\Documents and Settings\NetworkService "NETWORK SERVICE"

or

%SystemDrive%\Documents and Settings\USER_NAME, which translates directly to the "USERNAME" values in most un-tampered systems, ie. where the user has not changed the their user name after a few weeks or altered the paths explicitly...

spadge
  • 19
  • 1
  • 2
    ""%systemroot%\system32\config\systemprofile"... as its not a directory path..." is incorrect. Try c&p this into the run dialog. – user66001 Nov 23 '12 at 21:54