Ensure that a Windows batch file is run as Adminstrator

It is sometimes necessary to run Windows batch files as an Administrator. While this in itself isn’t possible 100% because of the obvious security risks, it is do-able!

Simply add this to the beginning of your bat file:

set IS_ELEVATED=0
whoami /groups | findstr /b /c:"Mandatory Label\High Mandatory Level" | findstr /c:"Enabled group" > nul: && set IS_ELEVATED=1
if %IS_ELEVATED%==0 (
cls
echo You must run the command prompt as administrator to install.
pause 5
exit /b 1
)

Every time the batch file is run as a non administrator it will display a message that you need to run as administrator and close itself upon a keypress

Print Friendly, PDF & Email

More Like This


Categories


Win 7 / 10 Tips & Tricks Windows