@echo off
setlocal EnableDelayedExpansion

:: ============================================================
:: DISM TOOL
:: DISM runs hidden and detached.
:: A notification reports the result and elapsed time.
:: ============================================================

:: Elevate if necessary
net session >nul 2>&1
if %errorlevel% neq 0 (
    powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process '%~f0' -Verb RunAs"
    exit /b
)

:MENU
cls
echo.
echo ============================================
echo             DISM TOOL
echo ============================================
echo.
echo  1. CheckHealth
echo  2. ScanHealth
echo  3. RestoreHealth
echo  4. StartComponentCleanup
echo  5. StartComponentCleanup + ResetBase
echo  6. AnalyzeComponentStore
echo.
set "CHOICE="
set /p "CHOICE=Selection: "

set "CMD="
set "OPERATION="

if "%CHOICE%"=="1" (
    set "CMD=/Online /Cleanup-Image /CheckHealth"
    set "OPERATION=CheckHealth"
)

if "%CHOICE%"=="2" (
    set "CMD=/Online /Cleanup-Image /ScanHealth"
    set "OPERATION=ScanHealth"
)

if "%CHOICE%"=="3" (
    set "CMD=/Online /Cleanup-Image /RestoreHealth"
    set "OPERATION=RestoreHealth"
)

if "%CHOICE%"=="4" (
    set "CMD=/Online /Cleanup-Image /StartComponentCleanup"
    set "OPERATION=StartComponentCleanup"
)

if "%CHOICE%"=="5" (
    set "CMD=/Online /Cleanup-Image /StartComponentCleanup /ResetBase"
    set "OPERATION=StartComponentCleanup + ResetBase"
)

if "%CHOICE%"=="6" (
    set "CMD=/Online /Cleanup-Image /AnalyzeComponentStore"
    set "OPERATION=AnalyzeComponentStore"
)

if not defined CMD goto MENU

echo.
echo ============================================
echo DISM operation:
echo %OPERATION%
echo ============================================
echo.
echo DISM will run hidden and detached.
echo You may close this window.
echo.
echo A notification will appear when it finishes.
echo.

:: Create detached PowerShell worker
set "WORKER=%TEMP%\DISM_Worker_%RANDOM%.ps1"

> "%WORKER%" echo $start = Get-Date
>>"%WORKER%" echo $p = Start-Process dism.exe -ArgumentList '%CMD%' -WindowStyle Hidden -Wait -PassThru
>>"%WORKER%" echo $elapsed = (Get-Date) - $start
>>"%WORKER%" echo $code = $p.ExitCode
>>"%WORKER%" echo $time = '{0:hh\:mm\:ss}' -f $elapsed
>>"%WORKER%" echo if ($code -eq 0) { $result = 'Success - operation completed successfully.' } elseif ($code -eq 3010) { $result = 'Success - operation completed; restart required.' } else { $result = 'DISM finished with exit code ' + $code + '.' }
>>"%WORKER%" echo Add-Type -AssemblyName PresentationFramework
>>"%WORKER%" echo [void][System.Windows.MessageBox]::Show(('DISM ' + '%OPERATION%' + [Environment]::NewLine + $result + [Environment]::NewLine + 'Exit code: ' + $code + [Environment]::NewLine + 'Elapsed time: ' + $time),'DISM')
>>"%WORKER%" echo Remove-Item -LiteralPath $MyInvocation.MyCommand.Path -Force

start "" /b powershell.exe -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File "%WORKER%"

echo.
echo Background DISM process started.
echo.
timeout /t 3 /nobreak >nul
exit /b
```
