[windows] How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?

I recently needed a user-friendly approach and I came up with this, based on valuable insights from contributors here and elsewhere. Simply put this line at the top of your .bat script. Feedback welcome.

@pushd %~dp0 & fltmc | find "." && (powershell start '%~f0' ' %*' -verb runas 2>nul && exit /b)

Intrepretation:

  • @pushd %~dp0 ensures a consistant working directory relative to this batch file; supports UNC paths
  • fltmc a native windows command that outputs an error if run unelevated
  • | find "." makes the error prettier, and causes nothing to output when elevated
  • && ( if we successfully got an error because we're not elevated, do this...
  • powershell start invoke PowerShell and call the Start-Process cmdlet (start is an alias)
  • '%~f0' pass in the full path and name of this .bat file. Single quotes allow spaces in the path/file name
  • ' %*' pass in any and all arguments to this .bat file. Funky quoting and escape sequences probably won't work, but simple quoted strings should. The leading space is needed to prevent breaking things if no arguments are present
  • -verb runas don't just start this process...RunAs Administrator!
  • 2>nul discard PowerShell's unsightly error output if the UAC prompt is canceled/ignored.
  • && if we successfully invoked ourself with PowerShell, then...
    • NOTE: in the event we don't obtain elevation (user cancels UAC), then && allows the .bat to continue running without elevation, such that any commands that require it will fail but others will work just fine. If you want the script to simply exit instead of running unelevated, make this a single ampersand: &
  • exit /b) exits the initial .bat processing, because we don't need it anymore; we have a new elevated process currently running out .bat. Adding /b allows cmd.exe to remain open if the .bat was started from the command line...it has no effect if the .bat was double-clicked

Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to batch-file

'ls' is not recognized as an internal or external command, operable program or batch file '' is not recognized as an internal or external command, operable program or batch file XCOPY: Overwrite all without prompt in BATCH CanĀ“t run .bat file under windows 10 Execute a batch file on a remote PC using a batch file on local PC Windows batch - concatenate multiple text files into one How do I create a shortcut via command-line in Windows? Getting Error:JRE_HOME variable is not defined correctly when trying to run startup.bat of Apache-Tomcat Curl not recognized as an internal or external command, operable program or batch file Best way to script remote SSH commands in Batch (Windows)

Examples related to windows-10

Starting ssh-agent on Windows 10 fails: "unable to start ssh-agent service, error :1058" How to install OpenSSL in windows 10? Anaconda Installed but Cannot Launch Navigator Conda command is not recognized on Windows 10 Copy Files from Windows to the Ubuntu Subsystem Hyper-V: Create shared folder between host and guest with internal network Why Local Users and Groups is missing in Computer Management on Windows 10 Home? VMware Workstation and Device/Credential Guard are not compatible pip not working in Python Installation in Windows 10 Copy Paste in Bash on Ubuntu on Windows

Examples related to uac

XAMPP installation on Win 8.1 with UAC Warning Can you force Visual Studio to always run as an Administrator in Windows 8? How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required? How to run a program automatically as admin on Windows 7 at startup? HttpListener Access Denied How to request Administrator access inside a batch file Requested registry access is not allowed Request UAC elevation from within a Python script?

Examples related to elevated-privileges

How to run python script with elevated privilege on windows How to run vbs as administrator from vbs? How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required? How do I force my .NET application to run as administrator? How to request Administrator access inside a batch file