Menu
Active1 year ago
- Creating Batch File In Xp
- Execute Batch File As Administrator Program
- Batch Run Exe As Admin
- Execute Batch File As Administrator Windows 7
- Batch File Administrator Permissions
This does not work for me on either Windows 10 or Windows 7. When I try to run as Administrator, either by right clicking the BAT file and 'Run as Administrator', or using the technique described here the batch file flashes open for a second then closes immediately with no commands or programs in the batch file. Step #3 states to run the script with admin privileges. If this is not set then the whole thing won't run with administrator rights and the script will fail. If you tested the batch file using the command line then you will have to run the command line with admin privileges as well, though that has nothing to do with Step #3.
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.
- When we run the batch file from the command prompt (running as administrator), the batch file runs successfully on all above combinations. Success means the word doc is converted to HTML format successfully. When we run the batch file using the task scheduler, it fails on combination #4.
- Aug 01, 2018 Batch Files As Administrator. To auto-run a batch file as administrator, you need to first create the batch file, and then create a shortcut for it. To create a shortcut, right-click the file, and select Send to.
- 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.
Is this possible to do differently?
Jim Fell5,8442828 gold badges108108 silver badges183183 bronze badges
skbskb12.5k2929 gold badges8787 silver badges135135 bronze badges
11 Answers
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/batchgotadmin
dbenham107k2020 gold badges191191 silver badges295295 bronze badges
Ben GripkaBen Gripka12.5k55 gold badges3636 silver badges3737 bronze badges
Here's a one-liner I've been using:
Notes:
- Only tested on windows 7 and 10, you might have to mess around with the quoting
- Doesn't support passing along arguments for now
1,36911 gold badge1717 silver badges2525 bronze badges
Here is my code! It looks big but it is mostly comment lines (the lines starting with ::).
Features:
- 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
Community♦
cyberponkcyberponk
Another approach is to
- create a shortcut locally and set it to call for Admin permission [Properties, Advanced, Run as Admin]
and then
Creating Batch File In Xp
- send your users the shortcut [or a link to the shortcut rather than one to the batch file itself].
Denis
[Added afterwards - Yes, I did fail to notice the date of this thread.]
Tryx3Tryx3
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.
fishbonefishbone1,46722 gold badges2121 silver badges3737 bronze badges
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.
trex005trex005Execute Batch File As Administrator Program
3,92011 gold badge1717 silver badges3535 bronze badges
@echo off
and title
can come before this code:Batch Run Exe As Admin
A lot of the other answers are overkill if you don't need to worry about the following:
- Parameters
- Working Directory (
cd %~dp0
will change to the directory containing the batch file)
FluorescentGreen5FluorescentGreen5
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
Community♦
barbara.postbarbara.post
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][1]
His post helped for configuring optional argument while scheduling the task.
AnandAnand
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 verb
AndersAndersExecute Batch File As Administrator Windows 7
75.3k1010 gold badges8181 silver badges133133 bronze badges
Batch File Administrator Permissions
use the runas command. But, I don't think you can email a .bat file easily.
JoshJosh
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?
Would you like to answer one of these unanswered questions instead?