0

The question "How to request Administrator access inside a batch file" has already been answered and I use this script. However, on a system with two user accounts: a normal user account and an admin user account I want to run my script from the normal user account, have it ask for admin privilege, then run the main part of my script (so far so good).

In the main part of my script I want to use the path to the home folder of the normal user account. I tried %UserProfile% but since this is now running as the admin user account I get the home folder of the admin instead.

I tried changing the set params= %* line to set params= "%UserProfile%" %* so that it passes the path of the home folder in to the Administrator cmd instance as a parameter but the quotes make it crash for some reason. I need the quotes in case the normal user account name has a space in it.

How can I get this working?

Jonathan
  • 39
  • 1
  • 6
  • The user shouldn't change. You are requesting elevated privileges, not becoming another user. For that reason `%UserProfile%` should still resolve to the same location. Other than that, the main difference is that the current directory changes to `%SystemRoot%\System32`. – Compo Jun 03 '23 at 01:26
  • @Compo When I `echo %UserProfile%` after the privilege escalation, it prints the profile path of the admin user. I'm on Windows 10 22H2 and just created a new `User1` non-admin account to test with. I'm using local Windows accounts. – Jonathan Jun 05 '23 at 20:44
  • Which 'admin' user? Your command did not specify one! Are you sure this is a local user and not a domain user? What happens when you create a batch file containing just `@Set User & Pause`, right click on it, and select 'Run as administrator'? – Compo Jun 05 '23 at 21:06
  • By admin user I just meant a user account that has admin rights. This is not a domain user. This is a Home version of Windows. My admin user account is `MATE-3` and the PC's hostname is `MATE-PC-3`. Here's the output of that command when selecting 'Run as administrator' from the `User1` user account. USERDOMAIN=MATE-PC-3 USERDOMAIN_ROAMINGPROFILE=MATE-PC-3 USERNAME=MATE-3 USERPROFILE=C:\Users\MATE-3 – Jonathan Jun 05 '23 at 21:20
  • Sorry about the formatting. – Jonathan Jun 05 '23 at 21:23
  • Even if I open a regular command prompt as administrator and run the command `echo %UserProfile%` it gives me the path of the administrator's home directory, though I'm logged into the non-administrator account. – Jonathan Jun 06 '23 at 00:59

0 Answers0