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
485 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 69

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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
For loop to add numbers 3 85
How to run multiple lines in a batch file 4 76
how to run a batch process 16 50
Nmap how to find wich type of socks is this? 4 107
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

820 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