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
482 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 39

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 39

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 39

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 39

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 39

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 39

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 39

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

786 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