Registry location for home folder

Posted on 2011-10-05
Last Modified: 2012-05-12
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?
Question by:qisadmin
    LVL 8

    Accepted Solution

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

    Assisted Solution

    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

    Author Comment

    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


    Author Comment

    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


    Author Comment

    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.

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Installing a printer using group policy preferences is not that hard let’s take a look at it. First lets open up your group policy console and edit the policy you want to add it to. I recommend creating a new policy for each printer makes it a l…
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now