@echo off
setlocal
title Activate Microsoft Windows
cls

REM :: Check for administrator rights and self-elevate if necessary
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
    echo.
    echo [ERROR] Administrator privileges are required to run this script.
    echo         Requesting elevation...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"="
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs"
    pushd "%CD%"
    CD /D "%~dp0"
    cls
    echo ============================================================================
    echo  Microsoft Windows Activation Script (v5 - Final)
    echo ============================================================================
    echo.
    echo This script will attempt to activate your version of Windows using
    echo a public KMS server.
    echo.
    echo Running with administrative privileges...
    echo ============================================================================
    echo.

set "GVLK_KEY="
set "ProductName="
set "DetectedProductName="

for /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v EditionID') do set "EditionID=%%b"
for /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName') do set "DetectedProductName=%%b"

if defined DetectedProductName (
    echo Automatically detected Windows version: %DetectedProductName%
    echo.
) else (
    echo [ERROR] Could not automatically detect Windows version.
    goto :manual_menu
)

rem --- Robust auto-detection logic using string substitution ---
if not "%DetectedProductName:11=%"=="%DetectedProductName%" (
    if "%EditionID%"=="Professional" set "GVLK_KEY=W269N-WFGWX-YVC9B-4J6C9-T83GX" & set "ProductName=Windows 11 Professional"
    if "%EditionID%"=="Enterprise" set "GVLK_KEY=NPPR9-FWDCX-D2C8J-H872K-2YT43" & set "ProductName=Windows 11 Enterprise"
    if "%EditionID%"=="Core" set "GVLK_KEY=TX9XD-98N7V-6WMQ6-BX7FG-H8Q99" & set "ProductName=Windows 11 Home"
    if "%EditionID%"=="CoreSingleLanguage" set "GVLK_KEY=7HNRX-D7KGG-3K4RQ-4WPJ4-YTDFH" & set "ProductName=Windows 11 Home Single Language"
    if "%EditionID%"=="Education" set "GVLK_KEY=NW6C2-QMPVW-D7KKK-3GKT6-VCFB2" & set "ProductName=Windows 11 Education"
    if "%EditionID%"=="ProfessionalWorkstation" set "GVLK_KEY=NRG8B-VKK3Q-CXVCJ-9G2XF-6Q84J" & set "ProductName=Windows 11 Pro for Workstations"
    if "%EditionID%"=="ProfessionalEducation" set "GVLK_KEY=6TP4R-GNPTD-KYYHQ-7B7DP-J447Y" & set "ProductName=Windows 11 Pro Education"
)

if not "%DetectedProductName:10=%"=="%DetectedProductName%" (
    if "%EditionID%"=="Professional" set "GVLK_KEY=W269N-WFGWX-YVC9B-4J6C9-T83GX" & set "ProductName=Windows 10 Professional"
    if "%EditionID%"=="Enterprise" set "GVLK_KEY=NPPR9-FWDCX-D2C8J-H872K-2YT43" & set "ProductName=Windows 10 Enterprise"
    if "%EditionID%"=="Core" set "GVLK_KEY=TX9XD-98N7V-6WMQ6-BX7FG-H8Q99" & set "ProductName=Windows 10 Home"
    if "%EditionID%"=="CoreSingleLanguage" set "GVLK_KEY=7HNRX-D7KGG-3K4RQ-4WPJ4-YTDFH" & set "ProductName=Windows 10 Home Single Language"
    if "%EditionID%"=="Education" set "GVLK_KEY=NW6C2-QMPVW-D7KKK-3GKT6-VCFB2" & set "ProductName=Windows 10 Education"
    if "%EditionID%"=="ProfessionalWorkstation" set "GVLK_KEY=NRG8B-VKK3Q-CXVCJ-9G2XF-6Q84J" & set "ProductName=Windows 10 Pro for Workstations"
    if "%EditionID%"=="ProfessionalEducation" set "GVLK_KEY=6TP4R-GNPTD-KYYHQ-7B7DP-J447Y" & set "ProductName=Windows 10 Pro Education"
    if "%EditionID%"=="EnterpriseS" set "GVLK_KEY=M7XTQ-FN8P6-TTKYV-9D4CC-J462D" & set "ProductName=Windows 10 Enterprise LTSC"
	if "%EditionID%"=="IoTEnterprise" set "GVLK_KEY=KBN8V-HFGQ4-MGXVD-347P6-PDQGT" & set "ProductName=Windows 10 IoT Enterprise"
)

if "%EditionID%"=="ServerStandard" (
    if not "%DetectedProductName:2022=%"=="%DetectedProductName%" set "GVLK_KEY=VDYBN-27WPP-V4HQT-9VMD4-VMK7H" & set "ProductName=Windows Server 2022 Standard"
    if not "%DetectedProductName:2019=%"=="%DetectedProductName%" set "GVLK_KEY=N69G4-B89J2-4G8F4-WWYCC-J464C" & set "ProductName=Windows Server 2019 Standard"
)
if "%EditionID%"=="ServerDatacenter" (
    if not "%DetectedProductName:2022=%"=="%DetectedProductName%" set "GVLK_KEY=WX4NM-KYWYW-QJJR4-XV3QB-6VM33" & set "ProductName=Windows Server 2022 Datacenter"
    if not "%DetectedProductName:2019=%"=="%DetectedProductName%" set "GVLK_KEY=WMDGN-G9PQG-XVVXX-R3X43-63DFG" & set "ProductName=Windows Server 2019 Datacenter"
)


if not defined GVLK_KEY (
    echo [WARNING] Your Windows version was not recognized automatically.
    echo Please select your version from the list below.
    echo.
    goto :manual_menu
)

goto :activate

:manual_menu
echo Please select your Windows version from the list below:
echo.
echo  --- Windows 11 ---
echo  [1] Windows 11 Pro
echo  [2] Windows 11 Enterprise
echo  [3] Windows 11 Home
echo  [4] Windows 11 Education
echo.
echo  --- Windows 10 ---
echo  [5] Windows 10 Pro
echo  [6] Windows 10 Enterprise
echo  [7] Windows 10 Home
echo  [8] Windows 10 Education
echo  [9] Windows 10 Enterprise LTSC
echo.
echo  --- Windows Server ---
echo  [10] Windows Server 2022 Datacenter
echo  [11] Windows Server 2022 Standard
echo  [12] Windows Server 2019 Datacenter
echo  [13] Windows Server 2019 Standard
echo.
echo  [14] Exit
echo.

set /p "choice=Enter your choice [1-14]: "

if "%choice%"=="1" set "GVLK_KEY=W269N-WFGWX-YVC9B-4J6C9-T83GX" & set "ProductName=Windows 11 Pro"
if "%choice%"=="2" set "GVLK_KEY=NPPR9-FWDCX-D2C8J-H872K-2YT43" & set "ProductName=Windows 11 Enterprise"
if "%choice%"=="3" set "GVLK_KEY=TX9XD-98N7V-6WMQ6-BX7FG-H8Q99" & set "ProductName=Windows 11 Home"
if "%choice%"=="4" set "GVLK_KEY=NW6C2-QMPVW-D7KKK-3GKT6-VCFB2" & set "ProductName=Windows 11 Education"
if "%choice%"=="5" set "GVLK_KEY=W269N-WFGWX-YVC9B-4J6C9-T83GX" & set "ProductName=Windows 10 Pro"
if "%choice%"=="6" set "GVLK_KEY=NPPR9-FWDCX-D2C8J-H872K-2YT43" & set "ProductName=Windows 10 Enterprise"
if "%choice%"=="7" set "GVLK_KEY=TX9XD-98N7V-6WMQ6-BX7FG-H8Q99" & set "ProductName=Windows 10 Home"
if "%choice%"=="8" set "GVLK_KEY=NW6C2-QMPVW-D7KKK-3GKT6-VCFB2" & set "ProductName=Windows 10 Education"
if "%choice%"=="9" set "GVLK_KEY=M7XTQ-FN8P6-TTKYV-9D4CC-J462D" & set "ProductName=Windows 10 Enterprise LTSC"
if "%choice%"=="10" set "GVLK_KEY=WX4NM-KYWYW-QJJR4-XV3QB-6VM33" & set "ProductName=Windows Server 2022 Datacenter"
if "%choice%"=="11" set "GVLK_KEY=VDYBN-27WPP-V4HQT-9VMD4-VMK7H" & set "ProductName=Windows Server 2022 Standard"
if "%choice%"=="12" set "GVLK_KEY=WMDGN-G9PQG-XVVXX-R3X43-63DFG" & set "ProductName=Windows Server 2019 Datacenter"
if "%choice%"=="13" set "GVLK_KEY=N69G4-B89J2-4G8F4-WWYCC-J464C" & set "ProductName=Windows Server 2019 Standard"
if "%choice%"=="14" goto :end


if not defined GVLK_KEY (
    echo.
    echo [ERROR] Invalid choice. Please try again.
    echo.
    goto :manual_menu
)

:activate
echo.
echo ============================================================================
echo  Starting activation for: %ProductName%
echo ============================================================================
echo.

rem Use full path to slmgr.vbs for robustness
set "slmgr=cscript //nologo %windir%\\system32\\slmgr.vbs"

echo [1/3] Installing product key...
%slmgr% /ipk %GVLK_KEY%
echo.

echo [2/3] Setting KMS server address...
%slmgr% /skms kms.digiboy.ir
echo.

echo [3/3] Attempting to activate...
%slmgr% /ato
echo.

:end
echo ============================================================================
echo Activation attempt finished. Check the status messages above.
echo ============================================================================
echo.
pause