How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?

I want my batch file to only run elevated. If not elevated, provide an option for the user to relaunch batch as elevated.

I’m writing a batch file to set a system variable, copy two files to a Program Files location, and start a driver installer. If a Windows 7/Windows Vista user (UAC enabled and even if they are a local admin) runs it without right-clicking and selecting “Run as Administrator”, they will get ‘Access Denied’ copying the two files and writing the system variable.

I would like to use a command to automatically restart the batch as elevated if the user is in fact an administrator. Otherwise, if they are not an administrator, I want to tell them that they need administrator privileges to run the batch file. I’m using xcopy to copy the files and REG ADD to write the system variable. I’m using those commands to deal with possible Windows XP machines. I’ve found similar questions on this topic, but nothing that deals with relaunching a batch file as elevated.

18 Answers
18

Leave a Comment