Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file: Drop elevated privileges (run a command as original user)

Tags:

I have a batch file that starts with elevated privileges (my installer spawns it), but at a certain point I need to run a command as the original user who started my installer (i.e. drop from the elevated privileges).

Is it possible to do so?

like image 340
sashoalm Avatar asked Nov 26 '13 13:11

sashoalm


People also ask

How do you run a command with elevated privileges?

In the search results window, under Programs, right-click on the program cmd.exe. In the pop-up menu, select Run As Administrator. If a User Access Control window appears, log in with a Windows user account that has full Administrator access rights. An Elevated Command Prompt window should now open.


1 Answers

You can run a command with restricted privileges with:

runas /trustlevel:0x20000 "YourCommandHere"

You should provide the absolute path to your command including any arguments in double quotes as an argument to runas.

If you would like to run more than one command with restricted privileges, you can put them in a separate batch file and run it with:

runas /trustlevel:0x20000 "cmd /C PathToYourBatchFile"

Anyway, this will open a new console with restricted privileges. You also have to use this syntax whenever you wish to run with restricted privileges an internal command (like copy, del, etc.) as these are provided by the command line interpreter and do not have an associated path.

Note that 0x20000 is the trust level of standard users. You can list other available trust levels by running

runas /showtrustlevels
like image 159
GOTO 0 Avatar answered Oct 20 '22 16:10

GOTO 0