1

In Vista, I am trying to get "Local AppData" path for an user account (other than current user) on a local machine but facing some issue. can anyone pls help me what is wrong with the below code.

var HKU = 0x80000003; 
var username = "xyz";

//Loading registry hive of user xyz
var WshShell = new ActiveXObject("WScript.Shell");
var LoadHiveCmd = "REG LOAD " + "HKU" + "\\" + username + " \"" + "c:\\users\\xyz\\NTUSER.DAT" + "\"";
var oExec = WshShell.Exec(strLoadHiveCmd); 

var oReg = GetObject("WinMgmts:/root/default:StdRegProv"); 

var profileRegPath = username  + "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders";

var method, inparams, outparams;  

method = oReg.Methods_.Item("GetExpandedStringValue");
inparams = method.InParameters.SpawnInstance_();
inparams.hDefKey = HKU;
inparams.sSubKeyName = profileRegPath ;
inparams.sValueName =  "Local AppData";

outparams = oReg.ExecMethod_(method.Name, inparams);
var appDataPath= outparams.sValue;   

Here the appDataPath value in the registry is %USERPROFILE%\AppData\Local

But I am getting a value C:\Windows\system32\config\systemprofile\AppData\Local

I dont understand from where the value c:\windows\system32\config\systemprofile is coming and how it replaced %USERPROFILE% value.

svv
  • 13
  • 3
  • 1
    A Community user was nice enough to format your code for you. Please Have a look at [How to format code on StackOverflow](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) in order to assure you providing the code in an easy-to-read format for the helpers. – Dutchie432 May 04 '11 at 19:29

2 Answers2

0

USERPROFILE is a environment variable and will replace %USERPROFILE% to get it's correct location on this computer. it changes from computer to computer.

To see all environment variables type "set" on a command shell, or go to "Control Panel" > "System settings" > "Advanced" > Environment variables

ariel
  • 15,620
  • 12
  • 61
  • 73
  • The %USERPROFILE% environment value is not the one c:\windows\system32\config\systemprofile. When I checked, the value of userprofile for the current process is c:\users\admin. Couldn't understand where from this systemprofile string came – svv May 04 '11 at 19:38
  • When you open the registry editor, what branch you browse to? You have to go to HKEY_USERS\[xyz user id]\Software\... – ariel May 04 '11 at 19:46
  • ya.. i am going to HKU_USERS\Xyz branch only. But I am loading hive of xyz thru reg load and my current process is running with admin user. That is why the value of %userprofile% is c:\users\admin but not c:\users\stduser – svv May 04 '11 at 19:51
  • 1
    yes, USERPROFILE would only get the correct value if you log in using the user account. – ariel May 04 '11 at 19:54
0

GetExpandedStringValue automatically replaces any environment variables included in the registry value data with actual values of these variables. Most likely, %USERPROFILE% expands to C:\Windows\system32\config\systemprofile instead of C:\users\admin because the WMI service itself runs under Local System account.

What you need to get your script working is to:

  • use GetStringValue instead of GetExpandedStringValue to read the unexpanded Local AppData value,

  • get the profile path of the needed user by reading the ProfileImagePath value from the HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\<user_SID> key,

  • do a string replace to substitute %USERPROFILE% with the profile path.

You can find an example of how to do this in this my answer:
Getting special Folder path for a given user in Jscript

You may also want to use WshShell.RegRead instead of WMI, because it's more JScript-friendly.

Community
  • 1
  • 1
Helen
  • 87,344
  • 17
  • 243
  • 314
  • Unfortunately, GetStringValue behaves exactly like GetExpandedStringValue when used on a `REG_EXPAND_SZ` (I'm not sure what the difference is, actually) – CherryDT Jun 07 '17 at 14:37