@setlocal DisableDelayedExpansion
@echo off
if defined PROCESSOR_ARCHITEW6432 start "" %SystemRoot%\Sysnative\cmd.exe /c "%0 " &exit
set "SysPath=%SystemRoot%\System32"
set "Path=%SysPath%;%SystemRoot%;%SysPath%\Wbem;%SysPath%\WindowsPowerShell\v1.0\"

set "_err===== ERROR ===="
for /f "tokens=6 delims=[]. " %%# in ('ver') do if %%# geq 7600 goto :E_Win
reg query HKU\S-1-5-19 1>nul 2>nul || goto :E_Admin

set "_wrn===== WARNING ===="
set "_CBS=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages"
set "xOS=64"
set "xBT=amd64"
if /i %PROCESSOR_ARCHITECTURE%==x86 (if not defined PROCESSOR_ARCHITEW6432 (
  set "xOS=32"
  set "xBT=x86"
  )
)
set "_bat=%~f0"
set "_arg=%~1"
set "_elv="
if defined _arg if /i "%_arg%"=="-su" set _elv=1

set "_work=%~dp0"
set "_work=%_work:~0,-1%"
setlocal EnableDelayedExpansion
pushd "!_work!"
if not exist "%xOS%\" (
echo %_err%
echo Required folder %xOS% is missing.
goto :TheEnd
)
cd %xOS%\
for %%# in (
NSudoLC.exe
) do if not exist "%%~#" (
echo %_err%
echo Required file %xOS%\%%~nx# is missing.
goto :TheEnd
)

call :TIcmd 1>nul 2>nul
whoami /USER | find /i "S-1-5-18" 1>nul && (
goto :Begin
) || (
if defined _elv goto :E_TI
net start TrustedInstaller 1>nul 2>nul
1>nul 2>nul NSudoLC.exe -U:T -P:E cmd.exe /c ""!_bat!" -su" &exit /b
)
whoami /USER | find /i "S-1-5-18" 1>nul || goto :E_TI

:Begin
echo.
echo ____________________________________________________________
echo.
echo Checking Prerequisites . . .
echo.

set "PKGS=%SystemRoot%\Servicing\Packages"
if exist "%PKGS%\Microsoft-Windows-Foundation-Package*6.0.6001.18000.mum" goto :E_Win

if exist "%PKGS%\Package_25_for_KB4474419*.mum" reg query "%_CBS%\Package_25_for_KB4474419~31bf3856ad364e35~%xBT%~~6.0.4.0" /v CurrentState 2>nul | find /i "0x7" 1>nul && (
echo %_wrn%
echo WUC Package is already installed.
goto :TheEnd
)
if exist "%SystemRoot%\WinSxS\pending.xml" (
echo %_wrn%
echo Pending update operation detected.
echo Restart the system first, then run the script.
goto :TheEnd
)
if not exist "%PKGS%\Package_for_KB4474419*.mum" (
echo %_err%
echo Required update KB4474419 is missing.
echo You must install it first.
goto :TheEnd
)
if not exist "%PKGS%\Package_for_KB4493730*.mum" (
echo %_err%
echo Required update KB4493730 is missing.
echo You must install it first.
goto :TheEnd
)
set "_LPS=2:cs-CZ 3:de-DE 4:en-US 5:es-ES 6:fr-FR 7:hu-HU 8:it-IT 9:ja-JP 10:ko-KR 11:nl-NL 12:pl-PL 13:pt-BR 14:pt-PT 15:ru-RU 16:sv-SE 17:tr-TR 18:zh-CN 19:zh-HK 19:zh-TW"
set _spt=0
for %%# in (
%_LPS%
) do for /f "tokens=1,2 delims=:" %%A in ("%%#") do (
if exist "%PKGS%\Microsoft-Windows-Client-LanguagePack-Package*%%B*.mum" set _spt=1
)
if %_spt% equ 0 (
echo %_err%
echo No supported Language Pack detected.
echo Refer to ReadMe for more info.
goto :TheEnd
)

@cls
echo.
choice /C YN /N /M "WUC 7.7.6003.20555 will be installed. Continue? [y/n]: "
if errorlevel 2 exit

set "KSHA=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Servicing\Codesigning\SHA2"
reg query %KSHA% 2>nul | find /i "SHA2-Codesigning-Support" 1>nul || (
reg add %KSHA% /v SHA2-Codesigning-Support /t REG_DWORD /d 1 /f 1>nul 2>nul
reg add %KSHA% /v SHA2-Core-Codesigning-Support /t REG_DWORD /d 1 /f 1>nul 2>nul
)

echo.
echo ____________________________________________________________
echo.
echo Installing Windows Update Client . . .
echo.

call :StopService wuauserv 1>nul 2>nul
set errcode=0
for %%# in (
%_LPS%
) do for /f "tokens=1,2 delims=:" %%A in ("%%#") do (
if exist "%PKGS%\Microsoft-Windows-Client-LanguagePack-Package*%%B*.mum" (
  call :AddCBS %%B 1>nul 2>nul
  start /w PkgMgr.exe /ip /m:"WUC\Package_%%A_for_KB4474419~31bf3856ad364e35~%xBT%~~6.0.4.0.mum" /quiet /norestart 1>nul 2>nul
  call set errcode=!errorlevel!
  )
)
if %errcode% neq 0 if %errcode% neq 3010 (
echo.&echo %_err%
echo Installing language packages failed.
goto :Failure
)
for /L %%# in (2,1,19) do if exist "%PKGS%\Package_%%#_for_KB4474419~31bf3856ad364e35~%xBT%~~6.0.4.0.mum" (
reg add "%_CBS%\Package_%%#_for_KB4474419~31bf3856ad364e35~%xBT%~~6.0.4.0" /v Visibility /t REG_DWORD /d 2 /f 1>nul 2>nul
)
call :AddCBS 1>nul 2>nul
start /w PkgMgr.exe /ip /m:"WUC\Package_25_for_KB4474419~31bf3856ad364e35~%xBT%~~6.0.4.0.mum" /quiet /norestart 1>nul 2>nul
call set errcode=%errorlevel%
if %errcode% neq 0 if %errcode% neq 3010 (
echo.&echo %_err%
echo Installing main package failed.
goto :Failure
)
reg add "%_CBS%\Package_25_for_KB4474419~31bf3856ad364e35~%xBT%~~6.0.4.0" /v Visibility /t REG_DWORD /d 2 /f 1>nul 2>nul
call :RemCBS 1>nul 2>nul

echo.
echo ____________________________________________________________
echo.
echo Done.
if exist "%SystemRoot%\WinSxS\pending.xml" (
echo System restart is required to complete the process.
)
goto :TheEnd

:AddCBS
if "%1"=="" (
set "pkg=Microsoft-Windows-Foundation-Package~31bf3856ad364e35~%xBT%~~6.0.6001.18000"
) else (
set "pkg=Microsoft-Windows-Client-LanguagePack-Package~31bf3856ad364e35~%xBT%~%1~6.0.6001.18000"
)
copy /y CBS\%pkg%.* %PKGS%
reg add "%_CBS%\%pkg%" /f /v CurrentState /t REG_DWORD /d 0x7 &reg add "%_CBS%\%pkg%" /f /v InstallClient /t REG_SZ /d "Package Manager" &reg add "%_CBS%\%pkg%" /f /v InstallLocation /t REG_SZ /d \\?\C:\Packages\ &reg add "%_CBS%\%pkg%" /f /v InstallName /t REG_SZ /d %pkg%.mum &reg add "%_CBS%\%pkg%" /f /v InstallTimeHigh /t REG_DWORD /d 0x1c6fe94 &reg add "%_CBS%\%pkg%" /f /v InstallTimeLow /t REG_DWORD /d 0x5b437864 &reg add "%_CBS%\%pkg%" /f /v InstallUser /t REG_SZ /d S-1-5-18 &reg add "%_CBS%\%pkg%" /f /v SelfUpdate /t REG_DWORD /d 0x0 &reg add "%_CBS%\%pkg%" /f /v Trusted /t REG_DWORD /d 0x1 &reg add "%_CBS%\%pkg%" /f /v Visibility /t REG_DWORD /d 0x1 &reg add "%_CBS%\%pkg%\Owners" /f /v %pkg% /t REG_DWORD /d 0x20007
exit /b

:RemCBS
del /f /q %PKGS%\Microsoft-Windows-Foundation-Package~31bf3856ad364e35~%xBT%~~6.0.6001.18000.*
reg delete "%_CBS%\Microsoft-Windows-Foundation-Package~31bf3856ad364e35~%xBT%~~6.0.6001.18000" /f
for %%# in (
%_LPS%
) do for /f "tokens=1,2 delims=:" %%A in ("%%#") do (
del /f /q %PKGS%\Microsoft-Windows-Client-LanguagePack-Package~31bf3856ad364e35~%xBT%~%%B~6.0.6001.18000.*
reg delete "%_CBS%\Microsoft-Windows-Client-LanguagePack-Package~31bf3856ad364e35~%xBT%~%%B~6.0.6001.18000" /f
)
exit /b

:StopService
sc query %1 | find /i "STOPPED" || net stop %1 /y
sc query %1 | find /i "STOPPED" || sc stop %1
exit /b

:TIcmd
reg add HKU\.DEFAULT\Console /f /v FaceName /t REG_SZ /d Consolas
reg add HKU\.DEFAULT\Console /f /v FontFamily /t REG_DWORD /d 0x36
reg add HKU\.DEFAULT\Console /f /v FontSize /t REG_DWORD /d 0x100000
reg add HKU\.DEFAULT\Console /f /v FontWeight /t REG_DWORD /d 0x190
reg add HKU\.DEFAULT\Console /f /v ScreenBufferSize /t REG_DWORD /d 0x12c0050
exit /b

:E_TI
echo %_err%
echo Failed running the script with TrustedInstaller privileges.
goto :TheEnd

:E_Admin
echo %_err%
echo This script requires administrator privileges.
goto :TheEnd

:E_Win
echo %_err%
echo This project is for Windows Vista only.
goto :TheEnd

:Failure
if exist "%PKGS%\Package_25_for_KB4474419~31bf3856ad364e35~%xBT%~~6.0.4.0.mum" (
start /w PkgMgr.exe /up:"Package_25_for_KB4474419~31bf3856ad364e35~%xBT%~~6.0.4.0" /quiet /norestart 1>nul 2>nul
)
for /L %%# in (2,1,19) do if exist "%PKGS%\Package_%%#_for_KB4474419~31bf3856ad364e35~%xBT%~~6.0.4.0.mum" (
start /w PkgMgr.exe /up:"Package_%%#_for_KB4474419~31bf3856ad364e35~%xBT%~~6.0.4.0" /quiet /norestart 1>nul 2>nul
)
call :RemCBS 1>nul 2>nul

:TheEnd
echo.
echo Press any key to exit.
pause >nul
goto :eof
