How to Elevate Privileges from Command Line


My task has been pretty simple: I have needed to run a batch file with a lot of Robocopy commands. The issue I have faced is usual for Windows Vista, Windows 7 or Windows Server 2008:

ERROR: You do not have the Backup and Restore Files user rights.

Yes, if I right click on the batch file and select Run As Administrator, everything works fine. My goal has been to run this batch file from the command line without user interaction at all. At the same time I have needed not to compromise security

RunAs does not help at all. In order to get the privileges elevated the Administrator user has to be used, but this user is disabled by default and I do not want to enable it.

I have tried to change batch file properties and set up Run This Program As an Administrator, but this checkbox has been disabled. I have done this for Robocopy.exe, but it makes situation even worse: UAC asks its question every time the batch file runs robocopy.

Finally, I have found the solution

I have created a scheduled task called RunBatch to run my batch file. I have not set up any schedule for it; I have checked the Run on highest privileges box on General tab and saved the task.

The command

SCHTASKS /run /tn RunBatch

runs the job with elevated privileges and without asking any question

6 Responses to How to Elevate Privileges from Command Line

  1. Antonio says:

    Awesome trick! Thank you!

  2. Michael K says:

    sänks! saved my day!

  3. pera says:

    Great tip! Thanks!

  4. Anonymous says:

    nice one

  5. M. Kuziak says:

    I have never used Scheduled Tasks, just used batch/cmd files ad hoc. I have wasted most of a day trying to solve the problem you described here. Could you reply letting me know what your file Runbatch contains so I could modify it for myself?. Is it a batch file? I suspect the /tn switch is a Scheduled Task switch. I will look that up. Thanks.

    • Runbatch is simply a task name, that you can create using GUI in Task Scheduler. The /tn option stands for TaskName.

      Try SCHTASKS /? and SCHTASKS /Run /? to get more information about this command.

Leave a comment