RunAs Administrator

Force a batch to run as different user via RunAs command.

Description: Force a batch to run as different user via RunAs command. This snippet checks the USERNAME environment variable. If it`s not `Administrator` then it automatically restart the batch with Administrator credentials using the RunAs command. Administrator password is required.
Script: Download: DosRunAsAdmin.bat  
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
@echo off
echo.Current User is '%USERNAME%'

rem -- Let's make sure this batch runs as Administrator --
set "RunAsUser=Administrator"
if "%USERNAME%" NEQ "%RunAsUser%" (
    RUNAS /user:%RunAsUser% "cmd /c %~f0"||PAUSE
    GOTO:EOF
)

rem -- your code goes below here --
echo.Hello World

ECHO.&PAUSE&GOTO:EOF
Script Output:
 DOS Script Output
Current User is 'DosItHelp'
Enter the password for Administrator:
Attempting to start cmd /c C:\dostips\DosRunAsAdmin.bat as user "DOSTIPS_PC\Administrator" ...


---- RunAs now opens a new window ----

Current User is 'Administrator'
Hello World

Press any key to continue . . .