[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • Last Modified:

How to get the path of the profile for default logon user in Windows by using DOS batch?

Hi all,

We have a couple of workstations in various sites. Some are using Windows XP Professional and some are using Windows 7 Professional/Enterprise.

All of these workstations are:
1) not in a domain;
2) having default logon user in "Users" group with no password set (i.e. when the computer starts up, it will go to that user directly); and
3) remote desktop are not enabled for security reasons.

Now, I would like to write a single DOS batch script and give the script to the staff at each site to update an entry under HKCU key for the default logon user for each workstation at their site such that they can perform another specific task (not related to this question) on each workstation.

However, the default logon user have a different user name for each of the public workstation, and the entry cannot be updated by the default logon user by default.

Therefore, my idea is to logon to the local administrator account on each workstation and run the script to import the HKCU key of the default logon user and update the specific entry.

The following is what I got so far.  
SET NTUSRPATH="C:\Users\XXX\ntuser.dat"
reg load HKLM\TempHive %NTUSERPATH%
reg add <Key_Name>  /v <Value_Name> /t REG_DWORD /d 0 /f 

Open in new window


However, I have no idea on getting the path to the ntuser.dat of the default logon user.

Besides, as shown in the given code, it is working for Windows 7 only.  I know that for Windows XP, the path is under C:\Documents and Settings. Is it possible to set a single value for the NTUSRPATH that works for both Windows XP and Windows 7? If no, how can I distinguish the current OS to be Windows XP and Windows 7?

Thank you.
0
Peter Kwan
Asked:
Peter Kwan
  • 9
  • 8
1 Solution
 
als315Commented:
You can check if ntuser.dat exist in some path:
IF EXIST "C:\Users\XXX\ntuser.dat" SET NTUSRPATH="C:\Users\XXX\ntuser.dat"
IF EXIST "C:\Documents and Settings\XXX\ntuser.dat" SET NTUSRPATH="C:\Documents and Settings\XXX\ntuser.dat"
0
 
QlemoDeveloperCommented:
The basic path of the current user's profile is stored in %UserProfile%. That can be used to check for the correct path:
setlocal EnableDelayedExpansion
set NTUSRPATH="%UserProfile:!UserName!=XXX%\ntuser.dat"

Open in new window

or
for %%D in ("%UserProfile%") do set NTUSRPATH="%~dpDXXX\ntuser.dat"

Open in new window

But the IF EXIST approach should work the same.
0
 
Peter KwanAuthor Commented:
Hi, Qlemo,

For the first code segment, it still shows the path to the current user profile.

For the second code segment, it shows the following error message:

The following usage of the path operator in batch-parameter
substitution is invalid: %~dpDXXX\ntuser.dat"

For valid formats type CALL /? or FOR /?

Besides, as stated in the question, the default logon user have a different user name for each of the public workstation, and I have no idea what the user names are (since the total number of workstations are over 500 at over 60 sites, I need to make the script as generic as possible such that the staff at the site can run the script and work properly at each workstation.).

 What can I substitute for XXX in the script?

Thank you.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
als315Commented:
May be you can use runas and start cmd file from current user?:
SET NTUSRPATH=%Userprofile%\ntuser.dat
runas /user:domain\adminname "c:\test.cmd "%NTUSRPATH%

test.cmd could be:
reg load HKLM\TempHive %1
reg add <Key_Name>  /v <Value_Name> /t REG_DWORD /d 0 /f
0
 
Peter KwanAuthor Commented:
Hi als315,

Thanks. Tried your method, but it seems that the registry is not updated.
0
 
als315Commented:
If it is windows7, you should run it as administrator
0
 
Peter KwanAuthor Commented:
Just tried to save the following in the batch script in Windows 7:

SET NTUSRPATH=%Userprofile%\ntuser.dat
runas /user:computername\admin "c:\test.cmd "%NTUSRPATH%

and run the script as administrator. The value is still not updated.
0
 
als315Commented:
Has it asked for admin password? Try in test.cmd to start from:
echo %1 > c:\tmp\log.txt
correct file name to real path.
0
 
Peter KwanAuthor Commented:
It has asked the password. I typed in the password and the log.txt file is created.

However, after reverting back to the code in ID:38773308 above and add the pause in the end of test.cmd, it is found that the following:

reg load HKLM\TempHive %1
ERROR: The process cannot access the file because it is being used by another process.

I guess it is because the ntuser.dat is locked when the current user is logged on.
0
 
als315Commented:
You can run one cmd from current user:
echo %Userprofile%\ntuser.dat > c:\tmp\user.txt
and second logged as admin:
reg load HKLM\TempHive < c:\tmp\user.txt
reg add <Key_Name>  /v <Value_Name> /t REG_DWORD /d 0 /f
0
 
Peter KwanAuthor Commented:
I have updated the test.cmd as said:

reg load HKLM\TempHive < c:\tmp\user.txt
reg add <Key_Name>  /v <Value_Name> /t REG_DWORD /d 0 /f

But Still failed.

C:\Windows\system32>reg load HKLM\TempHive  0<c:\tmp\user.txt
ERROR: Invalid syntax.
Type "REG LOAD /?" for usage.
0
 
als315Commented:
OK. Change first (user) cmd to:
echo SET NTUSRPATH=%Userprofile%\ntuser.dat > c:\tmp\user.cmd
second will be:
call c:\tmp\user.cmd
reg load HKLM\TempHive %NTUSERPATH%
reg add <Key_Name>  /v <Value_Name> /t REG_DWORD /d 0 /f
0
 
Peter KwanAuthor Commented:
Still not working:

C:\Windows\system32>call c:\tmp\user.cmd

C:\Windows\system32>SET NTUSRPATH=C:\Users\XXX\ntuser.dat

C:\Windows\system32>reg load HKLM\TempHive C:\Users\XXX\ntuser.dat
ERROR: The process cannot access the file because it is being used by another process.
0
 
als315Commented:
If you like to load profile from file, you should run first cmd, logout from current user, logon as administrator and run second cmd.
0
 
Peter KwanAuthor Commented:
I see.  Does this way works for XP as well?
0
 
als315Commented:
Can't test, but I hope
0
 
Peter KwanAuthor Commented:
Thanks all for your effort.  I found the solution as below:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "DefaultUserName"') DO SET USR=%%B

FOR /F "TOKENS=2,3 DELIMS=," %%A IN ('"WMIC USERACCOUNT GET NAME,SID /FORMAT:CSV"') DO (
IF "%%A"=="%USR%" (
FOR /F "TOKENS=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%%B" /v "ProfileImagePath"') DO (
IF NOT "x%%B"=="x" SET "USRPATH=%%B"
)
)
)

SET DRV_LETTER=C:
SET USRPATH=!USRPATH:%%SystemDrive%%=%DRV_LETTER%!
REG LOAD HKLM\TempHive "%USRPATH%\NTUSER.DAT"
reg add <Key_Name>  /v <Value_Name> /t REG_DWORD /d 0 /f 
REG UNLOAD HKLM\TempHive
ENDLOCAL

Open in new window


I just need to run using admin account and it works for both XP and 7.
0
 
Peter KwanAuthor Commented:
The proposed solutions from other experts does not fit my goal perfectly.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now