Registry Editor is a useful utility in Windows which allows users to easily change advanced Windows settings by altering registry keys present in a hierarchical arrangement called the Windows Registry. Despite being such a powerful tool, Registry Editor is not totally error-proof. A simple virus infection is all it takes to render it useless.
I have a small script that performs the build and install process on Windows for a Bazaar repository I'm managing. I'm trying to run the script with elevated, administrative privileges from within the Windows shell (cmd.exe)--just as if I'd right-clicked it and chosen Run as Administrator, but without using any method that requires use of the graphical interface.
jpaughjpaugh
8 Answers
A batch/WSH hybrid is able to call ShellExecute to display the UAC elevation dialog...
AndersAnders
Press the start button. In the search box type 'cmd', then press Ctrl+Shift+Enter
slashdottirslashdottir
All you have to do is use the
runas
command to run your program as Administrator (with a caveat).In my case, this was
Note that you must use Quotation marks, else the runas command will gobble up the switch option to cmd.
Also note that the administrative shell (cmd.exe) starts up in the C:WindowsSystem32 folder. This isn't what I wanted, but it was easy enough to pass in the current path to my installer, and to reference it using an absolute path.
Caveat: Enable the admin account
Using runas this way requires the administrative account to be enabled, which is not the default on Windows 7 or Vista. However, here is a great tutorial on how to enable it, in three different ways:
I myself enabled it by opening Administrative Tools, Local Security Policy, then navigating to Local PoliciesSecurity Options and changing the value of the Accounts: Administrative Account Status policy to Enabled, which is none of the three ways shown in the link.
An even easier way:
jpaughjpaugh
For a demo: self-elevating.bat 'path with spaces' arg2 3 4 'another long argument'
And this is another version that does not require creating a temp file.
Amr AliAmr Ali
I would set up a shortcut, either to CMD or to the thing you want to run, then set the properties of the shortcut to require admin, and then run the shortcut from your batch file. I haven't tested to confirm it will respect the properties, but I think it's more elegant and doesn't require activating the Administrator account.
Also if you do it as a scheduled task (which can be set up from code) there is an option to run it elevated there.
Kate GregoryKate Gregory
Simple pipe trick,
||
, with some .vbs used at top of your batch. It will exit regular and restart as administrator.It also
del /Q
the temp.vbs when it's done using it.strancestrance
Although @amr ali's code was great, I had an instance where my bat file contained
>
<
signs, and it choked on them for some reason.I found this instead. Just put it all before your code, and it works perfectly.
KapantherKapanther
Browse to
C:windowsSystem32
and right click on cmd.exe
and run as Administrator. Worked for me on Windows 7.If you are trying to run a script with elevated privileges you could do the same for the script file or use the scheduler's run as a different user option to run the script.
![Runas command powershell Runas command powershell](http://www.liutilities.com/products/registrybooster/tweaklibrary/tweaks/10404/p10404_1.png)
RasikaRasika
protected by Community♦May 15 '17 at 8:20
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?