Skip to content
Home

Open batch file as admin

This example tries to open the hosts file. If unsuccessfull, then prompts for Administrator password. Checkout the video here

Source codes

Using temporary VBS script

REM Test if Admin
CALL NET SESSION >nul 2>&1
IF NOT %ERRORLEVEL% == 0 (
rem Start batch again with UAC
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
rem Program will now start again automatically with admin rights!
rem pause
goto :eof
)
echo Started with Admin privilliges...
CD C:\Windows\System32\drivers\etc
notepad hosts

Using powershell

Thanks to @temisociali

REM Test if Admin
if not "%1"=="am_admin" (
powershell -Command "Start-Process -Verb RunAs -FilePath '%0' -ArgumentList 'am_admin'"
exit /b
)
echo Started with Admin privilliges...
CD C:\Windows\System32\drivers\etc
notepad hosts