- Aug 08, 2016 Sometimes you need to create a batch file which includes commands that require elevated rights. Due to the UAC function included in Windows since Windows Vista, when start a batch file, it opens without the required access rights unless you right click it and select 'Run as Administrator' from the context menu.
- To elevate the batch file to run as admin, follow the steps below: Right-click the shortcut you just created (should be on the desktop or where ever you send it) Under the Shortcut tab, click the Advanced. Check the Run as administrator checkbox and press OK to both the modal window and the main properties window.
- The file is re-writing their hosts file, so it needs to be run with Administrator permissions. I need to be able to send them an email with a link to the.bat file. The desired behavior is that when they right-click on the file and say Open, they will get one of those UAC dialogs that makes the screen go dark and forces them to answer whether they want to give the application permission to run as administrator.
1 A batch file learning if it is run as administrator. 1.1 Run a batch file only if administrator; 1.2 Run a batch file only if not administrator; 2 Elevate Privileges; 3 BatchGotAdmin (Windows 8) 3.1 An example script to create a directory symlink; 3.2 An example script to create many symlinks. Because with Bat files, you should open a Command Prompt (Admin) By pressing Windows key+X, then after it is open, CD to the folder where the BAT files are, and then run it. That way, you can see if it ran or if it failed. Opening an elevated command prompt, as you describe, and as I mentioned, does work.
I am trying to write a batch file for my users to run from their Vista machines with UAC. The file is re-writing their hosts file, so it needs to be run with Administrator permissions. I need to be able to send them an email with a link to the .bat file. The desired behavior is that when they right-click on the file and say Open, they will get one of those UAC dialogs that makes the screen go dark and forces them to answer whether they want to give the application permission to run as administrator. Instead, they are just seeing 'Access denied' on the command line window.
Is this possible to do differently?Jim Fell
This script does the trick! Just paste it into the top of your bat file. If you want to review the output of your script, add a 'pause' command at the bottom of your batch file.
UPDATE: This script is now slightly edited to support command line arguments and a 64 bit OS.
Thank you Eneerge @ https://sites.google.com/site/eneerge/scripts/batchgotadmindbenham
Here's a one-liner I've been using:
- Only tested on windows 7 and 10, you might have to mess around with the quoting
- Doesn't support passing along arguments for now
Here is my code! It looks big but it is mostly comment lines (the lines starting with ::).
- Full argument forwarding
- Does not change working folder
- Error handling
- Accepts paths with parenthesis (except for %TEMP% folder)
- Supports UNC paths
Mapped folder check (Warn´s you if admin can´t access mapped drive)
Can be used as an external library (check my post at this topic: https://stackoverflow.com/a/30417025/4932683)
- Can be called when/if needed anywhere in your code
Just attach this to the end of your batch file, or save it as a library (check above)
Example on how to use it
Another approach is to
- create a shortcut locally and set it to call for Admin permission [Properties, Advanced, Run as Admin]
- send your users the shortcut [or a link to the shortcut rather than one to the batch file itself].
Batch File Samples
[Added afterwards - Yes, I did fail to notice the date of this thread.]
Ben Gripka's solution causes infinite loops. His batch works like this (pseudo code):
As you can see, this causes an infinite loop, if the VBS fails requesting admin privileges.
However, the infinite loop can occur, although admin priviliges have been requested successfully.
The check in Ben Gripka's batch file is just error-prone. I played around with the batch and observed that admin privileges are available although the check failed. Interestingly, the check worked as expected, if I started the batch file from windows explorer, but it didn't when I started it from my IDE.
So I suggest to use two separate batch files. The first generates the VBS that calls the second batch file:
The second, named 'my_commands.bat' and located in the same directory as the first contains your actual commands:
This causes no infinite loops and also removes the error-prone admin privilege check.fishbonefishbone
I know this is not a solution for OP, but since I'm sure there are many other use cases here, I thought I would share.
I've had problems with all the code examples in these answers but then I found :http://www.robotronic.de/runasspcEn.html
It not only allows you to run as admin, it checks the file to make sure it has not been tampered with and stores the needed information securely. I'll admit it's not the most obvious tool to figure out how to use but for those of us writing code it should be simple enough.trex005trex005
@echo off and
title can come before this code:
A lot of the other answers are overkill if you don't need to worry about the following:
- Working Directory (
cd %~dp0will change to the directory containing the batch file)
Since I have troubles with this script popping up a new command prompt with itself run again, in infinite loop (using Win 7 Pro), I suggest you try another approach :How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?
Be careful, you have to add this at the end of script, as stated in an edit, so that you are back to script directory after privileges were elevated : cd /d %~dp0
Based on post by toster-cx and other interesting posts on this page, I got insight on how to configure and solve my problem. I had similar issue where I wished that Disk Cleanup utility runs every week twice on Monday and Thursday during lunch hours (say 2 pm). However, this required elevated rights.
Sharing batch file which might help other beginners like me -
Thanks a lot for this forum and Rems POST here [https://www.petri.com/forums/forum/windows-scripting/general-scripting/32313-schtasks-exe-need-to-pass-parameters-to-script]
His post helped for configuring optional argument while scheduling the task.
Execute Batch File With Admin Privileges
You can't request admin rights from a batch file, but you could write a windows scripting host script in %temp% and run that (and that in turn executes your batch as admin) You want to call the ShellExecute method in the Shell.Application object with 'runas' as the verbAndersAnders
use the runas command. But, I don't think you can email a .bat file easily.
protected by Community♦Nov 27 '12 at 20:12
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged batch-filecommand-lineuacelevated-privileges or ask your own question.
I have create a batch file which use to install my program as windows services.Content of the batch file:
Currently it needs the user to right-click the batch file and 'Run as Administrator' in order to success. How do we avoid 'Run as Administrator'? I mean can we use some command in the batch file to tell Windows to run this batch file as administrator?
Execute Batch File As Administrator Cmd
This way worked for me in the past:
Note that I'm running the commands in your batch file directly here, but of course you can also run the batch file itself:Master_TMaster_T