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
475 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 39

Expert Comment

by:als315
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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
 
LVL 39

Expert Comment

by:als315
Comment Utility
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
Comment Utility
Hi als315,

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

Expert Comment

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

Author Comment

by:Peter Kwan
Comment Utility
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 39

Expert Comment

by:als315
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Expert Comment

by:als315
Comment Utility
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
Comment Utility
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 39

Expert Comment

by:als315
Comment Utility
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
Comment Utility
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 39

Expert Comment

by:als315
Comment Utility
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
Comment Utility
I see.  Does this way works for XP as well?
0
 
LVL 39

Expert Comment

by:als315
Comment Utility
Can't test, but I hope
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 0 total points
Comment Utility
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
Comment Utility
The proposed solutions from other experts does not fit my goal perfectly.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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 …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

10 Experts available now in Live!

Get 1:1 Help Now