RunElevated.bat: Run an Elevated command on Windows
Posted by jpluimers on 2025/09/02
For a long time, I have ran with the runelevated.bat in [Wayback/Archive] Run an Elevated command using that: “net file” returns errorlevel 1 when not UAC, and “PowerShell Start-Process” has a “-Verb RunAs“; see the answers at http://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-admin-rights for more information
By now, I would just prepend this oneliner into each batch-file needing elevation:
@pushd "%~dp0" & fltmc | find "." && (powershell start '"%~f0"' ' %*' -verb runas 2>nul && popd && exit /b)
Both the initial batch file and one-liner are from [Wayback/Archive] windows – How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required? – Stack Overflow (thanks [Wayback/Archive] Ir Relevant, [Wayback/Archive] ceztko, [Wayback/Archive] Jamesfo, and [Wayback/Archive] PDixon724)
Note that the net file trick below should actually be repeated twice. This is explained in [Wayback/Archive] windows – Batch script: how to check for admin rights – Stack Overflow (thanks [Wayback/Archive] zumalifeguard), but wait: there is even a better solution!
The fltmc trick above works much better than the net file trick and is available from Windows XP and up, see [Wayback/Archive] windows – Batch script: how to check for admin rights – Stack Overflow (thanks [Wayback/Archive] npocmaka).
Oh: on systems where I have full installation control, I always install gsudo, see gsudo (sudo for windows).
–jeroen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :checkParameters | |
| if [%1]==[] ( goto :help ) else ( goto :checkPrivileges ) | |
| :help | |
| echo Syntax: | |
| echo %0 CmdLine | |
| echo Where CmdLine is executed with UAC privileges. | |
| goto :exit | |
| :checkPrivileges | |
| net file 1>nul 2>nul | |
| net file 1>nul 2>nul | |
| if '%errorlevel%' == '0' ( goto :gotPrivileges ) else ( goto :getPrivileges ) | |
| :getPrivileges | |
| PowerShell Start-Process -FilePath "%0" -Verb RunAs -ArgumentList %* | |
| goto :exit | |
| :gotPrivileges | |
| %* | |
| :exit | |
| pause | |
| exit /b |






Leave a comment