Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-12
18
Medium Priority
?
506 Views
Last Modified: 2013-02-10
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
Comment
Question by:Peter Kwan
  • 9
  • 8
18 Comments
 
LVL 40

Expert Comment

by:als315
ID: 38769948
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 38771985
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
 
LVL 16

Author Comment

by:Peter Kwan
ID: 38773122
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 40

Expert Comment

by:als315
ID: 38773308
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
 
LVL 16

Author Comment

by:Peter Kwan
ID: 38773375
Hi als315,

Thanks. Tried your method, but it seems that the registry is not updated.
0
 
LVL 40

Expert Comment

by:als315
ID: 38773469
If it is windows7, you should run it as administrator
0
 
LVL 16

Author Comment

by:Peter Kwan
ID: 38773520
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
 
LVL 40

Expert Comment

by:als315
ID: 38773592
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
 
LVL 16

Author Comment

by:Peter Kwan
ID: 38776889
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
 
LVL 40

Expert Comment

by:als315
ID: 38777021
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
 
LVL 16

Author Comment

by:Peter Kwan
ID: 38777147
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
 
LVL 40

Expert Comment

by:als315
ID: 38777219
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
 
LVL 16

Author Comment

by:Peter Kwan
ID: 38777229
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
 
LVL 40

Expert Comment

by:als315
ID: 38777277
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
 
LVL 16

Author Comment

by:Peter Kwan
ID: 38777342
I see.  Does this way works for XP as well?
0
 
LVL 40

Expert Comment

by:als315
ID: 38777390
Can't test, but I hope
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 0 total points
ID: 38858102
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
 
LVL 16

Author Closing Comment

by:Peter Kwan
ID: 38872831
The proposed solutions from other experts does not fit my goal perfectly.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

877 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