Batch file with commands to run as administrator and standard user
Solution 1:
You can add the following to your script and it will force it to run elevated. No need to download anything.
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
Solution 2:
I split my code into two parts
- one that required admin privileges.
renaming an executable installed in program files
- the other part that started and killed said executable.
starting it elevated rendered the program useless as the other things it relied on weren't elevated
made a shortcut for the bat file that required elevation and checked the "run as administrator" in its properties. used the shortcut in the other file.
CONS
- each shortcut opens a new cmd window
- I presume extra work is required if both parts depend on each other
I went to such lengths as the executable was running automatically despite disabling auto-startup in its settings and msconfig also set its service to manual. This was my last-ditch effort to make it behave.