Skip to content

Instantly share code, notes, and snippets.

@neuralpain
Last active February 13, 2024 14:06
Show Gist options
  • Save neuralpain/4bcc08065fe79e4597eb65ed707be90d to your computer and use it in GitHub Desktop.
Save neuralpain/4bcc08065fe79e4597eb65ed707be90d to your computer and use it in GitHub Desktop.
Invoke UAC prompt for Windows Batch scripts.
:: fsutil [...] is a universal method for checking
:: administrator privilages. works from Windows XP
:: and onwards
fsutil dirty query %systemdrive% >nul
:: if error, we do not have admin.
if %ERRORLEVEL% NEQ 0 (
cls & echo.
echo This script requires administrative priviledges.
echo Attempting to elevate...
goto UAC_Prompt
) else goto :begin_script
:UAC_Prompt
set n=%0 %*
set n=%n:"=" ^& Chr(34) ^& "%
echo Set objShell = CreateObject("Shell.Application")>"%tmp%\cmdUAC.vbs"
echo objShell.ShellExecute "cmd.exe", "/c start " ^& Chr(34) ^& "." ^& Chr(34) ^& " /d " ^& Chr(34) ^& "%CD%" ^& Chr(34) ^& " cmd /c %n%", "", "runas", ^1>>"%tmp%\cmdUAC.vbs"
cscript "%tmp%\cmdUAC.vbs" //Nologo
del "%tmp%\cmdUAC.vbs"
goto :eof
:begin_script
:: TODO...
exit
@neuralpain
Copy link
Author

neuralpain commented Dec 11, 2022

Simple guide

  • copy lines 5-21
  • paste into your current batch code
  • replace begin_script (line 12) with the label of your script body

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment