@echo off

:: ============================================================
::  Windows 10 IoT LTSC 2021  ->  22H2 Enablement Package
::  Toggle Script (Install / Remove)
:: ============================================================


:: --- Self-elevate to Administrator ----------------------------
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo Requesting administrator privileges...
    powershell -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b
)



:: --- Variables ------------------------------------------------ 
set "PKG_FOLDER=C:\22H2-update-files" ::CHANGE THE PATH TO THE FOLDER WITH YOUR EXTRACTED FILES FROM THE .CAB FILE
set "PKG_FILE=microsoft-windows-22h2enablement-package~31bf3856ad364e35~amd64~~10.0.19041.1799.mum"
set "PKG_NAME=microsoft-windows-22h2enablement-package~31bf3856ad364e35~amd64~~10.0.19041.1799"

:: --- Menu -----------------------------------------------------
:MENU
cls
echo ============================================================
echo   Windows 10 IoT LTSC 2021  ^|  22H2 Enablement Toggle
echo ============================================================
echo.
echo   [1]  Install   
echo   [2]  Uninstall 
echo   [3]  Exit
echo.
echo   NOTE: A restart will be required after either operation.
echo ============================================================
echo.
set /p "CHOICE=Enter your choice (1 / 2 / 3): "

if "%CHOICE%"=="1" goto INSTALL
if "%CHOICE%"=="2" goto UNINSTALL
if "%CHOICE%"=="3" exit /b
echo   Invalid choice. Please try again.
timeout /t 2 >nul
goto MENU

:: --- Install --------------------------------------------------
:INSTALL
cls
echo ============================================================
echo   INSTALL  --  Applying 22H2 Enablement Package
echo ============================================================
echo.

if not exist "%PKG_FOLDER%\%PKG_FILE%" (
    echo   ERROR: Package file not found at:
    echo   %PKG_FOLDER%\%PKG_FILE%
    echo.
    echo   Please make sure you have extracted the CAB contents
    echo   to  %PKG_FOLDER%  before running this script.
    echo.
    pause
    goto MENU
)

echo   Running DISM. This may take a minute...
echo.
Dism /online /add-package /packagepath:"%PKG_FOLDER%\%PKG_FILE%"

echo.
if %errorlevel% equ 0 (
    echo   SUCCESS. The package was applied.
) else (
    echo   WARNING: DISM returned exit code %errorlevel%.
    echo   Check the output above for details.
)
echo.
pause
goto MENU

:: --- Uninstall ------------------------------------------------
:UNINSTALL
cls
echo ============================================================
echo   UNINSTALL  --  Removing 22H2 Enablement Package
echo ============================================================
echo.
echo   Running DISM. This may take a minute...
echo.
Dism /online /remove-package /packagename:"%PKG_NAME%"

echo.
if %errorlevel% equ 0 (
    echo   SUCCESS. The package was removed. Version will revert
    echo   to 21H2 (build 19044) after restart.
) else (
    echo   WARNING: DISM returned exit code %errorlevel%.
    echo   The package may not be installed, or another error occurred.
)
echo.
pause
goto MENU
