Skip to content

Instantly share code, notes, and snippets.

@KaioHSG
Last active May 21, 2024 15:21
Show Gist options
  • Save KaioHSG/cdf636a71046583fc690b5c4dd48d25c to your computer and use it in GitHub Desktop.
Save KaioHSG/cdf636a71046583fc690b5c4dd48d25c to your computer and use it in GitHub Desktop.
UAC Credential: require administrator privileges in a script.
:: Start
@echo off
title UAC Credential
:: UAC Credential
whoami /groups | find "S-1-16-12288" > nul
if %ErrorLevel% neq 0 (
:: Non-admin message
echo Accept Administrator Privileges to continue.
echo Set UAC = CreateObject^("Shell.Application"^) > "%Temp%\UAC %~n0.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%Temp%\UAC %~n0.vbs"
"%Temp%\UAC %~n0.vbs"
del "%Temp%\UAC %~n0.vbs"
exit
)
:: Script folder
pushd "%~dp0"
:: Script
echo Running with administrator privileges in "%CD%".
pause > nul

UAC Credential

Require administrator privileges in a script.

How to use?

  1. Just paste the code below at the beginning of your script (preferably as high as possible).
whoami /groups | find "S-1-16-12288" > nul
if %ErrorLevel% neq 0 (
   echo Set UAC = CreateObject^("Shell.Application"^) > "%Temp%\UAC %~n0.vbs"
   echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%Temp%\UAC %~n0.vbs"
   "%Temp%\UAC %~n0.vbs"
   del "%Temp%\UAC %~n0.vbs"
   exit
)
  • You can also add texts below if %ErrorLevel% neq 0 (.
  whoami /groups | find "S-1-16-12288" > nul
  if %ErrorLevel% neq 0 (
+    echo Accept Administrator Privileges to continue.
     echo Set UAC = CreateObject^("Shell.Application"^) > "%Temp%\UAC %~n0.vbs"
     echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%Temp%\UAC %~n0.vbs"
     "%Temp%\UAC %~n0.vbs"
     del "%Temp%\UAC %~n0.vbs"
     exit
  )
  • You can add pushd "%~dp0" to go back to the script folder.
  whoami /groups | find "S-1-16-12288" > nul
  if %ErrorLevel% neq 0 (
     echo Set UAC = CreateObject^("Shell.Application"^) > "%Temp%\UAC %~n0.vbs"
     echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%Temp%\UAC %~n0.vbs"
     "%Temp%\UAC %~n0.vbs"
     del "%Temp%\UAC %~n0.vbs"
     exit
  )
+ pushd "%~dp0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment