The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

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



: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

view raw

RunElevated.bat

hosted with ❤ by GitHub

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.