Registry location for home folder

I really can't seem to understand this.

I'm trying to write a vbscript to search the registry for a specified user's home folder, but there is no absolutely consistent key I can find. I have seen the two following:

HKEY_USERS\<sid>\Volatile Environment\HOMESHARE and
HKEY_USERS\<sid>\Software\Microsoft\WIndows\CurrentVersion\Explorer\User Shell Folders\Personal

The problem is, neither one of these match (100% of the time) the variable located in the user's Active Directory "Profile" tab.

In every case, we have "\\server\users$\username" as the share mapped to the user's "H" drive. But on any given server (we have over 200 to support) the above keys can vary. For example: On a test server we have here, the HOMESHARE key is set to "\\server\users$", while the "Personal" key is what I am looking for. On another server, the HOMESHARE is what I want, and the "Personal" key has the share, but with "My Documents" appended to the end.

So how in the hell can I programatically find a user's share (at \\server\users$\username) while I'm not logged in as that user?
Who is Participating?
I think you might have actually touched on the correct answer yourself. Pull it from Active Directory rather than the registry.
AD itself would be more authoritative over any possible locally cached location of a user who is not currently logged in.  Assuming one domain, an example command line to get the home folder for a given user would be:
dsquery * -filter "(samaccountname=insert_the_user_login_name_here)" -attr samaccountname homedirectory
qisadminAuthor Commented:
We have some older servers not using AD, but the local SAM database instead. I would need a neutral method that works on all servers, so we can deploy the script universally. Would WMI suffice for this?

I have the following WMI call already being used to get the user's SID. I wonder if there exists a way to get the info I want.

Function getSID(myUserName)
' This function uses WMI to find a user's SID.
' It is required in order to reference the user's
' branch of the HKEY_USERS registry area.

   Dim strComputer, objWMIService, objAccount

   strComputer = "."
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

   Set objAccount = objWMIService.Get _
       ("Win32_UserAccount.Name='" & myUserName & "',Domain='" & DOMAIN & "'")

   getSID = objAccount.SID
End Function

Open in new window

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

qisadminAuthor Commented:
I think I might have found it, thanks to both of your suggestions. I have a bit further testing to do though.

On Error Resume Next
Const myUserName = "test"
Const DOMAIN = "domain"

Dim strComputer, objWMIService, objAccount

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objProfile = objWMIService.Get _
   ("Win32_NetworkLoginProfile.Name='" & DOMAIN & "\" & myUserName & "'")

Wscript.echo objProfile.HomeDirectory

Open in new window

qisadminAuthor Commented:
OK then, this makes no sense. Some user accounts work, some do not. I commented out the "On error Resume Next" line, so sometimes I get a "Not Found" error. So this doesn't seem to be viable.
qisadminAuthor Commented:
OK. I figured it out. It works on users who are currently logged in, and this is perfectly OK, because my script only works on users who are logged in anyhow.

Thanks to the both of you for suggesting pulling from AD instead of the registry.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.