Answered by:

Request Elevation inside CMD/batch file

Question

Is it possible to request elevation inside a CMD/batch file? I have a Command Script( .cmd) and one of the command require admin right to run. I am NOT looking for right-click "Run as administrator", I would like the script itself to call the UAC prompt.

But the problem is, I woulld not know which user will be running the script.I am thinking of sending it to a bunch of guys. The UAC prompt can check their permission and if they are admin, all they have to do is say OK.

I need to do the same! Just to be clear: I don't know neither the current user name, nor the admin password. I just need to ask the user via UAC, and if he confirm, run the script ITSELF as if it as been opened via "Run as administrator" menu.

I tried different search terms to find a solution but I am running in circles as I am always redirected to the same articles which don't offer a solution (like this thread)

I don't want that the batch user needs to know beforehand that the scripts needs to be run with admin rights. The user should be able to just double click the script.

If the batch detects that it has no admin rights (which can be done easily by checking the %errorlevel% variable after a command like 'at > nul') I want the batch to ask for elevation before it proceeds.

W.t.h. is there no easy way to do this?

As a workaround I currently use an error message like "this script must be run as administrator" when I detect that I have no admin rights but this is not my preferred solution because it is not user friendly at all.