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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I think you might have actually touched on the correct answer yourself. Pull it from Active Directory rather than the registry.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.