I am making an unattended version of Windows 10 LTSB 2016 RS1 and I need certain BAT files to run with Admin privileges as the first thing when PC boots up for the first time after clean install. I can't figure out how to make it happen... These BAT files have 0 effect now because OS runs them without Admin privileges...
I even set UAC to the lowest settings and put BAT files into Low-Risk in Registry and it still launches it as User and not As Administrator. Its a Post-Setup thing, not part of Unattended, so maybe that's the problem... This is the script:
Might be worth mentioning some tweaks need to run in FirstLogon stage of setup, so i normally use FirstLogon.cmd and it only fails if the script code is borked.
Auto Elevate DOS Batch File to Run As Administrator in Windows 10 The provided code will create a special VBS file which will restart it if it is not running as Administrator. So, if you launch it with limited permissions, you will get a UAC prompt requesting you to elevate privileges before it runs its commands! Code: REM Add this to beginning of .bat (BATCH) file to Automatically check & get admin rights @echo off CLS ECHO. ECHO ============================= ECHO Running Admin shell ECHO ============================= :init setlocal DisableDelayedExpansion set "batchPath=%~0" for %%k in (%0) do set batchName=%%~nk set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs" setlocal EnableDelayedExpansion :checkPrivileges NET FILE 1>NUL 2>NUL if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) :getPrivileges if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges) ECHO. ECHO ************************************** ECHO Invoking UAC for Privilege Escalation ECHO ************************************** ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%" ECHO args = "ELEV " >> "%vbsGetPrivileges%" ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%" ECHO args = args ^& strArg ^& " " >> "%vbsGetPrivileges%" ECHO Next >> "%vbsGetPrivileges%" ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%" "%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %* exit /B :gotPrivileges setlocal & pushd . cd /d %~dp0 if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul & shift /1) ::START REM - Add BATCH commands you want to run here
Scripts running in setupcomplete or firstlogon stage of Windows Setup always run elevated. This was already refrained several times.
Thank you so much for this! If I already use SetupComplete.cmd for KMS_VL_ALL, then can I just add other commands at the end? I assume I have to add them before the "exit /b" line?