[Solved] Execute batch-file to start wifi hotspot as admin


The hardest part of this is to run a .bat file as admin automatically, without even right-clicking on it. You need to save this code as a .bat file:

@ECHO OFF
:: this tests if the file is running as admin
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (GOTO askAdmin)
GOTO gotAdmin
:askAdmin
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
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" )
::from this point you can execute your command as admin
netsh wlan set hostednetwork mode=allow ssid=AdHoc key=password
netsh wlan start hostednetwork

Note that this does show the “Run this program as admin?” prompt when started without administrative priviliges, but if you right-click this batch-file and choose run as admin it should immediately execute the command you want it to execute

1

solved Execute batch-file to start wifi hotspot as admin