4

This question is different from Run as administrator from command line on Windows 8 and I wrote why: I want to run a program a) as a different user b) get elevated privileges (which is additionally required to executing as admin as per UAC) and c) pass the original environment. The linkes article only covers a+b.

I just saw that "runas /user:admin /env cmd.exe" which used to work in WinXP does not work in Vista+ any more due to UAC: An additional elevation is required.

What is the best/shortest (in the sense that I do not need to install a program; copying small auxiliary exes would be OK) to achieve:

  • Starting an exe as admin (i.e., a different user in the admin group)
  • Additionally, with elevated privileges
  • Keep the environment of the current users
  • Permits to supply parameters to the executed program

I found the "Elevation PowerToys for Windows Vista" and tried:

runas /user:admin /env "%USR%\bin\Elevate.cmd \"%COMMANDER_EXE%\""

but this has one show-stopper and some shortcomings:

  • Even though I use "/env" the executed program does not inherit the environment
  • The 3-fold nested parameters makes it difficult to supply more complicated parameters to %COMMANDER_EXE%
  • I would prefer a normal/graphical window to enter the password rather than a cmd window
  • After the runas, the UAC dialog additionally pops up (would be good to have this together)
divB
  • 565
  • 1
  • 7
  • 25
  • If you look at my answer to the linked question, you can use UAC to elevate with your current account (rather than running as a different account) if you use PowerShell's `Start-Process` with `-Verb runas`. You'll probably still run into problems with nested params, though that could be made easier by just running it directly in PS rather than trying to run it here. Incidentally, cmd uses `^` as its escape character, and PS uses `\``, so ``\`` is wrong. – Bob Jun 30 '14 at 06:11
  • Ok, that works so far but how does it keep the environment of the calling user? If I use the example, replace notepad with cmd and enter "set", it contains the environment of the admin user - not of the calling user! – divB Jun 30 '14 at 06:24
  • Really? I just tried it, and I get my current user's environment. Of course, that's if you're elevating as the same user (which is what `-verb runas` is supposed to solve over the raw `runas /user` command) - if you're actually elevating into a *different* user's account, then, yea, the environment is probably going to be different. – Bob Jun 30 '14 at 06:27
  • Oh damn, yeah that's the case :-( Can you think of a way to fix this? (BTW I included that as first bullet "Starting an exe as admin (i.e., a different user in the admin group)") – divB Jun 30 '14 at 06:28
  • I'd guess there's no easy way of fixing it (short of saving all vars to a file and loading them again from the target program). Can you explain why you need to do this? Perhaps there's an alternative. – Bob Jun 30 '14 at 06:30
  • Because my user is not in the admin group. I run Total Commander with all settings etc. stored in the user profile. Once in a while I need to do something with admin privs. Now I want to create a button which restarts Total Commander with admin privs (same as if I select "Run as administrator" - the account is a different one). But the environment is required in order to "see" the same settings, layout etc. – divB Jun 30 '14 at 06:32

0 Answers0