Run a .BAT Script without the Need for "Run as Administrator"

Page 1 of 2 12 LastLast

  1. Posts : 78
    Windows 10 Home (22H2)
       #1

    Run a .BAT Script without the Need for "Run as Administrator"


    Hi all


    I created a small .BAT script that runs CHKDSK/F on several partitions,
    and when I run this script, I get "Access Denied", and "You need to run it as Admin"..

    If I RightClick the .BAT file and Choose "Run as Administrator", then the script runs well.


    My question:
    Is there something I can do, so I don't need to RightClick + "Run as Administrator" all the time?

    I tried searching in google, and found 1 solution which is to create a shortcut to the .BAT file,
    and set the shortcut to run it as Admin.


    It's nice, yet it means the need to create such shortcut for various .BAT files.

    Instead,
    I would like to ask If there's maybe some System Wide solution,
    for example,
    to tell Windows 10 that I would like .BAT files (in general) to run as Admin, and not worry about it..

    Maybe via the Registry, or Group Policy, or something like that.


    Anyone knows If such option exists?


    Thank you
      My Computer


  2. Posts : 2,800
    Windows 7 Pro
       #2

    Hi,

    I don't know about a system wide option like that. It would be very dangerous.

    You can try one of these methods to run your Batch file with elevated privilege.

    Create Elevated Shortcut without UAC prompt in Windows 10
      My Computers


  3. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #3

    Hello @spaceman5,

    You need to add Run as Administrator to the top of the Script. There is various code available that can achieve this. This is one method that I use . . .

    Code:
    
    setlocal EnableDelayedExpansion
    set "Params=%*"
    cd /d "%~dp0" && ( if exist "%Temp%\getadmin.vbs" del "%Temp%\getadmin.vbs") && fsutil dirty query %SystemDrive% 1>nul 2>nul || ( echo set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %Params%", "", "runas", 1 >> "%Temp%\getadmin.vbs" && "%Temp%\getadmin.vbs" && Exit /b)
    %WinDir%\system32\reg.exe query "HKU\S-1-5-19" 1>nul 2>nul || ( echo. & echo  ERROR: This Batch file MUST be run in an ELEVATED cmd prompt [ Administrator ] & echo. & echo         Right-click the Batch file and click ^<Run as Administrator^>. & echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit )
    

    Also . . .

    Code:
    setlocal EnableDelayedExpansion
    fsutil Dirty Query %SystemDrive% > nul && goto:[RunAs]
    echo CreateObject^("Shell.Application"^). ^
    ShellExecute "%~0","+","","RunAs",1 > "%Temp%\+.vbs" && "%Temp%\+.vbs" & Exit
    :[RunAs]

    You will find others on this Forum.

    I hope this helps.
      My Computer


  4. Posts : 8,111
    windows 10
       #4

    You can try runas /env /user:domain\Administrator <program.exe/command you want to execute>

    Or convert it to exe it shouldnt then ask BAT 2 EXE
      My Computer


  5. Posts : 78
    Windows 10 Home (22H2)
    Thread Starter
       #5

    Thank you all.

    From the above solutions, the simplest path is to create an .EXE.

    I will do it not via converting a .BAT, but instead, via recreating the script in C#, and compiling it to .EXE.
    (thus benefitting from a much better programming language, rather than a scripting language)
      My Computer


  6. Posts : 52
    PE
       #6

    spaceman5 said:
    From the above solutions, the simplest path is to create an .EXE.
    The simplest is to copy/paste the code @Paul Black posted above to the very top of your CMD/BAT file.. It will automatically ask UAC for admin rights.

    Or you can add the below single two lines (Consolidated version of his) to the first line of your script

    Code:
    setlocal EnableDelayedExpansion
    CD /D "%~dp0" && ( IF EXIST "%temp%\getadmin.vbs" DEL "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>NUL 2>NUL || (  ECHO Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k CD ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && EXIT /B )
    Recreating the batch in C or compiling seems like more work than copy paste, no?

    EDIT
    I carelessly forgot that you do need to have setlocal EnableDelayedExpansion above that single line, again its just a slightly consolidated line other posted before me, so for prudence I edited the above codebox in case you end up referencing this later to copy/paste.
    Last edited by BobOmb; 27 Dec 2021 at 22:52.
      My Computer


  7. Posts : 78
    Windows 10 Home (22H2)
    Thread Starter
       #7

    BobOmb said:
    Recreating the batch in C or compiling seems like more work than copy paste, no?
    In the short run, yes,
    but in the long run, this code piece is quite a complex command, quite hard to grasp and take control of.
    At least to me..

    So that's why I won't use it.


    BTW..

    BobOmb said:
    It will automatically ask UAC for admin rights.
    When I think of it, the whole problem is quite weird:
    I disabled UAC on that Windows,
    but it seems that disabling UAC affects programs, but does not affect .BAT files..
      My Computer


  8. Posts : 52
    PE
       #8

    spaceman5 said:
    it seems that disabling UAC affects programs, but does not affect .BAT files..
    No if UAC is off that command will just elevate itself (Your cmd file), it will only request to run if UAC is on otherwise it will just be admin mode.

    Basically what that line does is:
    fsutil dirty query results show if you are running as admin or not, if not it creates a temp visual basic script that reruns your batch file as admin.. thats all it does..

    It is a good fix if you are not compiling, its how I test cmd scripts that need to be admin if I intend to compile later on...

    If you want to just compile the batch script itself without writing it into another language you can use AdvancedBATtoEXE at:
    Advanced BAT to EXE Converter PRO - compile batch files to exe

    With that tool you only need check the checkbox for "Include Admin Manifest" on the last screen before compile and that sets your compiled EXE to always run as admin. (Might be easier than rewriting in C)
      My Computer


  9. Posts : 78
    Windows 10 Home (22H2)
    Thread Starter
       #9

    BobOmb said:
    If you want to just compile the batch script itself without writing it into another language you can use AdvancedBATtoEXE at:
    Advanced BAT to EXE Converter PRO - compile batch files to exe
    Doesn't your Antivirus scream when encounter an .EXE created by a BAT-to-EXE Converter of any kind?


    BobOmb said:
    Might be easier than rewriting in C
    C#, not C :)
      My Computer


  10. Posts : 14,022
    Win10 Pro and Home, Win11 Pro and Home, Win7, Linux Mint
       #10

    Just a note, rightfully the command should be CHKDSK /F, needs a space before the / part of the switch [some of the things left over from MS-DOS].
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 10:23.
Find Us




Windows 10 Forums