Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I can't disable UAC in Windows 7 and I want to change .exe option to run as administrator. I can right click on icon and goto compatibility then change this option through GUI but I have to do by .bat..... Can someone give me a demo .bat.

share|improve this question

migrated from stackoverflow.com Nov 23 '11 at 5:11

This question came from our site for professional and enthusiast programmers.

    
I don't believe such a possibility is even supposed to be there in Windows 7. Elevating rights programmatically is something that Windows XP allows you to do and a thing that can potentially be (and probably is) exploited by malware. If Windows 7's UAC allowed elevating rights programmatically without user's consent, that would seem to me like stepping on the same rake. –  Andriy M Nov 22 '11 at 13:36
1  
Do you just want to launch something from a batch script as admin. Or do you want to change the exe to always run as admin (like you can in the compatibility tab)? –  Dracs Nov 23 '11 at 5:14

4 Answers 4

Right-click the BAT file and press Create Shortcut. The shortcut will appear. You can edit the shortcut's properties and set it to always run as administrator. Of course, you have to run the BAT file through the shortcut now.

share|improve this answer

I don't think it's possible in plain windows without installing an additional utility to elevate priviledges from the command line.

If you accept bundling such an utility with your bat file (or installing it system-wide), check this out: http://www.winability.com/elevate/

share|improve this answer

Another method is to have the batchfile call a VBscript that then elevates the batchfile.

You can download the Elevate PowerToys which allow you to elevate batchfiles.

elevate -batchfile-

Another method is to create a self elevating script. This can be a fragile method but it basically assumes you have write access to the %temp%

It's a modified version of the one here.

@ECHO OFF
::Ask for admin access
if exist "admincheckOK.txt" goto adminOK1
del /Q admincheckOK.vbs
ECHO.
ECHO. Please wait...
echo.Set objShell = CreateObject("Shell.Application") > %TEMP%\admincheckOK.vbs
echo.Set FSO = CreateObject("Scripting.FileSystemObject") >> %TEMP%\admincheckOK.vbs
echo.strPath = FSO.GetParentFolderName (WScript.ScriptFullName) >> %TEMP%\admincheckOK.vbs
echo.If FSO.FileExists(%0) Then >> %temp%\admincheckOK.vbs
echo. Dim oShell >> %TEMP%\admincheckOK.vbs
echo. Set oShell = WScript.CreateObject ("WScript.Shell") >> %TEMP%\admincheckOK.vbs
echo. oShell.run "cmd.exe /c echo admincheckOK > admincheckOK.txt" >> %TEMP%\admincheckOK.vbs
echo. Set oShell = Nothing >> %TEMP%\admincheckOK.vbs
echo. objShell.ShellExecute "cmd.exe", " /c " ^& %0 ^& " ", "", "runas", 1 >> %TEMP%\admincheckOK.vbs
echo.Else >> %TEMP%\admincheckOK.vbs
echo. MsgBox "Script file not found" >> %TEMP%\admincheckOK.vbs
echo.End If >> %TEMP%\admincheckOK.vbs
cscript //B %TEMP%\admincheckOK.vbs
goto theend

::Admin Access allowed
:adminOK1
del /Q admincheckOK.txt
del /Q admincheckOK.vbs

::**Body of Batchfile**
::**Code you want elevated**
pause

REM Following statement required if Admin access denied
:theend
del /Q admincheckOK.vbs
share|improve this answer

For an exe, if you put a manifest in the same folder as the exe that requests elevation, every time anyone runs the app they will be prompted to elevate it. Deleting the file from the folder will put the behaviour back to normal.

The name of the file must be exename.exe.manifest, in other words if you have foo.exe then you must name the file foo.exe.manifest. The contents must look like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 
    <assemblyIdentity version="1.0.0.0"
            processorArchitecture="X86"
            name="appname"
            type="win32"/> 
<description>elevate execution level</description> 
   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
      <security>
         <requestedPrivileges>
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
         </requestedPrivileges>
      </security>
   </trustInfo>
</assembly>

(The name and description don't matter, feel free to make them match your app. Or not.)

This will not work if the exe has an embedded manifest requesting a lower execution level, but will work for an manifest-less application.

I'm not sure why you want to turn this on and off with a batch file, but you can easily do it by having your batch file rename this file to foo.exe.notmanifest and back to foo.exe.manifest.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.