Introduce a new batch file, Run-ScriptMenu.bat, that:
Elevates itself once via UAC at startup
Auto-discovers up to 9 .ps1 files in its folder
Presents a numbered, single-keystroke menu (0 = Exit)
Runs the selected script in the same elevated session
Loops back for multiple executions without re-prompting UAC
Uses Start-Process -Verb RunAs for elevation
Leverages CHOICE and delayed expansion for input handling
Sorts scripts alphabetically for predictable ordering
Exits cleanly on invalid input or when “0” is selected
56 lines
1.5 KiB
Batchfile
56 lines
1.5 KiB
Batchfile
@echo off
|
||
:: Run-ScriptMenu.bat — one-UAC, loop-back launcher for *.ps1 files
|
||
:: Place this BAT next to your scripts and double-click.
|
||
|
||
rem ----------------- self-elevate once -----------------
|
||
>nul 2>&1 "%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system"
|
||
if errorlevel 1 (
|
||
powershell -NoProfile -Command "Start-Process '%~f0' -Verb RunAs"
|
||
exit /b
|
||
)
|
||
|
||
title Run PowerShell Script (Administrator)
|
||
setlocal EnableDelayedExpansion
|
||
cd /d "%~dp0"
|
||
|
||
:Build
|
||
set "i=0"
|
||
for /f "delims=" %%F in ('dir /b /a:-d "*.ps1" ^| sort') do (
|
||
if !i! lss 9 (
|
||
set /a i+=1
|
||
set "script!i!=%%F"
|
||
)
|
||
)
|
||
if !i! equ 0 (
|
||
echo No .ps1 files found beside this BAT.
|
||
pause & exit /b
|
||
)
|
||
goto Menu
|
||
|
||
:Menu
|
||
cls
|
||
echo ---------------------------------------------
|
||
echo Run PowerShell Script (admin session)
|
||
echo ---------------------------------------------
|
||
for /L %%N in (1,1,!i!) do echo %%N. !script%%N!
|
||
echo 0. Exit
|
||
echo.
|
||
|
||
choice /c 0123456789 /n /m "Pick number (0-Exit): "
|
||
set /a idx=%errorlevel%-1
|
||
if !idx! equ 0 exit /b
|
||
if !idx! gtr !i! (
|
||
echo Invalid choice – try again.
|
||
timeout /t 1 >nul
|
||
goto Menu
|
||
)
|
||
|
||
set "file=!script%idx%!"
|
||
echo.
|
||
echo === Running "!file!" …
|
||
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0!file!"
|
||
echo -------------------------------------------------
|
||
echo Done. Press any key to return to the menu.
|
||
pause >nul
|
||
goto Menu
|