# MediaCreationTool.bat
*Source: https://gist.github.com/155b79dba8b8082445cc8587057f932a*
```batch
@call :start MediaCreationTool.bat - latest version at pastebin.com/bBw0Avc4 or git.io/MediaCreationTool.bat
:: Universal MCT wrapper script by AveYo - for all Windows 10 versions from 1507 to 21H1!
:: Nothing but Microsoft-hosted source links and no third-party tools - script just configures a xml and starts MCT!
:: Ingenious support for business editions (Enterprise / VL) selecting language, x86, x64 or AiO inside the MCT GUI!
:: Changelog: 2021.08.04 done fiddling
:: - create iso directly; enhanced dialogs; args from script name or commandline; older MCT quirks ironed out!
:: - 21H1: 19043.928 / 20H2: 19042.631 / 2004: 19041.572 / 1909: 18363.1139 / 1903: 18362.356 / 1809: 17763.379
:: uncomment to skip gui dialog for MCT choice: 1=1507 to 12=21H1 - or rename script: "21H1 MediaCreationTool.bat"
rem set MCT=2104
:: uncomment to start auto setup directly using suitable options - or rename script: "auto MediaCreationTool.bat"
rem set/a AUTO=1
:: uncomment to start iso file creation directly with options - or rename script: "iso 20H2 MediaCreationTool.bat"
rem set/a ISO=1
:: uncomment and change autodetected MediaEdition - or rename script: "enterprise iso 1909 MediaCreationTool.bat"
rem set EDITION=Enterprise
:: uncomment and change autodetected MediaLangCode - or rename script: "de-DE home 19H2 iso MediaCreationTool.bat"
rem set LANGCODE=en-US
:: uncomment and change autodetected MediaArch - or rename script: "x64 auto 21H1 Education MediaCreationTool.bat"
rem set ARCH=x64
:: uncomment and change autodetected KEY - or rename script / provide via commandline - not needed for generic keys
rem set KEY=NPPR9-FWDCX-D2C8J-H872K-2YT43
:: uncomment to disable online dynamic update on upgrade - or rename script: "no_update auto MediaCreationTool.bat"
rem set/a NO_UPDATE=1
:: uncomment to disable Windows.old undo [fast] - or rename script: "no_undo no_update auto MediaCreationTool.bat"
rem set/a NO_UNDO=1
:: uncomment to not add $OEM$\ + auto.cmd to media - or rename script: "no_oem iso Pro 2004 MediaCreationTool.bat"
rem set/a NO_OEM=1
:: comment to not use recommended windows 10 setup options that give the least amount of issues when doing upgrades
set OPTIONS=%OPTIONS% /Compat IgnoreWarning /CompactOS Disable /MigrateDrivers All /ResizeRecoveryPartition Disable /ShowOOBE None
:: comment to not postpone key entering / disable setup telemetry - note that /Eula is already handled in AUTO or ISO modes
set OPTIONS=%OPTIONS% /Pkey Defer /Telemetry Disable
:: comment to not unhide Enterprise for 1709+ in products.xml
set/a UNHIDE_BUSINESS=1
:: comment to not insert Enterprise esd links for 1607,1703 or update links for 1909,2004 in products.xml
set/a INSERT_BUSINESS=1
:: MCT Version choice dialog items
set VERSIONS= 1507, 1511, 1607, 1703, 1709, 1803, 1809, 1903 [19H1], 1909 [19H2], 2004 [20H1], 2009 [20H2], 2104 [21H1]
:: MCT Action choice dialog items
set ACTIONS= Auto Setup, Create ISO, Select in MCT
:begin
:: parse MCT choice from script name or commandline - accepts both formats: 1909 or 19H2 etc.
set V=1.1507 2.1511 3.1607 4.1703 5.1709 6.1803 7.1809 8.1903 9.1909 10.2004 11.2009 12.2104 8.19H1 9.19H2 10.20H1 11.20H2 12.21H1
for %%V in (%V%) do for %%/ in (%MCT% %~n0 %*) do if /i %%~xV equ .%%~/ set "MCT=%%~nV" & set "VID=%%~/"
if defined MCT if not defined VID set "MCT="
:: parse AUTO from script name or commandline - starts unnatended upgrade / in-place repair / cross-edition
for %%/ in (%~n0 %*) do if /i %%/ equ auto set/a AUTO=1
if defined AUTO set/a ACT=1 & if not defined MCT set/a MCT=12
:: parse ISO from script name or commandline - starts media creation with selection
for %%/ in (%~n0 %*) do if /i %%/ equ iso set/a ISO=1
if defined ISO if not defined AUTO set/a ACT=2 & if defined MCT set/a CREATE=1
:: parse EDITION from script name or commandline - accepts one of the staged editions in MCT install.esd - see sources\product.ini
set _=%EDITION% %~n0 %* & rem :: also accepts the alternative names: Home, HomeN, Pro, ProN, Edu, EduN
for %%/ in (%_:Home=Core% %_:Pro =Professional % %_:ProN=ProfessionalN% %_:Edu =Education % %_:EduN=EducationN%) do (
for %%E in ( ProfessionalEducation ProfessionalEducationN ProfessionalWorkstation ProfessionalWorkstationN
Core CoreN CoreSingleLanguage CoreCountrySpecific Professional ProfessionalN Education EducationN Enterprise EnterpriseN
) do if /i %%/ equ %%E set "EDITION=%%E")
:: parse LANGCODE from script name or commandline - accepts any words starting with xy-
for %%/ in (%~n0 %*) do set ".=%%~/" & for /f %%C in ('cmd/q/v:on/recho;!.:~2^,1!') do if "%%C" equ "-" set "LANGCODE=%%/"
:: parse ARCH from script name or commandline - no, it does not accept "both"
for %%/ in (%~n0 %*) do for %%A in (x86 x64) do if /i %%/ equ %%A set "ARCH=%%A"
:: parse KEY from script name or commandline - accepts the format: AAAAA-VVVVV-EEEEE-YYYYY-OOOOO
for %%/ in (%KEY% %~n0 %*) do for /f "tokens=1-5 delims=-" %%a in ("%%/") do if "%%e" neq "" set "PKEY=%%/"
if defined PKEY set "PKEY1=%PKEY:~-1%" & set "PKEY28=%PKEY:~28,1%"
if defined EDITION if "%PKEY1%" equ "%PKEY28%" set "KEY=%PKEY%"
:: parse NO_UPDATE from script name or commandline - dont download and apply latest LCU on upgrade [more C:\ space and might fail]
for %%/ in (%~n0 %*) do if /i %%/ equ no_update set "NO_UPDATE=1"
if defined NO_UPDATE (set UPDATE=/DynamicUpdate Disable) else (set UPDATE=/DynamicUpdate Enable)
:: parse NO_UNDO from script name or commandline - dont create Windows.old undo data on upgrade [faster but less reliable]
for %%/ in (%~n0 %*) do if /i %%/ equ no_undo set "NO_UNDO=1"
if defined NO_UNDO (set UNDO=/Uninstall Disable) else (set UNDO=/Uninstall Enable)
:: parse NO_OEM from script name or commandline - dont include $OEM$\ subfolder and auto.cmd in the created media
for %%/ in (%~n0 %*) do if /i %%/ equ no_oem set "NO_OEM=1"
:: hint: setup can run a tweaking script before first logon, if present at $OEM$\$\Setup\Scripts\setupcomplete.cmd
:: detect current os_arch, os_langcode, os_edition, os_product, os_version
set "OS_ARCH=x64" & if "%PROCESSOR_ARCHITECTURE:~-2%" equ "86" if not defined PROCESSOR_ARCHITEW6432 set "OS_ARCH=x86"
call :reg_query "HKU\S-1-5-18\Control Panel\Desktop\MuiCached" "MachinePreferredUILanguages" OS_LANGCODE
for %%s in (%OS_LANGCODE%) do set "OS_LANGCODE=%%s"
call :reg_query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" "EditionID" OS_EDITION
call :reg_query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" "ProductName" OS_PRODUCT
call :reg_query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentBuildNumber" OS_VERSION
for %%/ in (1507.10240 1511.10586 1607.14393 1703.15063 1709.16299 1803.17134 1809.17763 1903.18362 1909.18363 2004.19041
2009.19042 2104.19043) do if .%OS_VERSION% equ %%~x/ set/a OS_VERSION=%%~n/
:: show more responsive MCT + ACT pseudo-menu dialog or separate choice dialog instances if either MCT or ACT are set
if "%MCT%;%ACT%"==";" call :choice.2x MCT "%VERSIONS%" 12 "MCT Version" ACT "%ACTIONS%" 2 "MCT Action" 11 white dodgerblue 360
if %MCT%0 lss 1 if %ACT%0 gtr 1 call :choice MCT "%VERSIONS%" 12 "MCT Version" 11 white dodgerblue 360
if %MCT%0 gtr 1 if %ACT%0 lss 1 call :choice ACT "%ACTIONS%" 2 "%VID% Action" 11 white dodgerblue 360
if %MCT%0 gtr 1 if %ACT%0 lss 1 goto choice-0 = cancel
goto choice-%MCT%
:choice-12
set "VER=2104" & set "VID=21H1" & set "CB=19043.928.210409-1212.21h1_release_svc_refresh" & set "CD=2021/04/" & set "CC=1.4.1"
set "CAB=%\\%download.microsoft.com/download/f/d/d/fddbe550-0dbf-44b4-9e60-6f0e73d654c0/products_20210415.cab"
set "EXE=%\\%download.microsoft.com/download/d/5/2/d528a4e0-03f3-452d-a98e-3e479226d166/MediaCreationTool21H1.exe"
:: refreshed 19041 base with integrated 21H1 enablement package - release
goto process
:choice-11
set "VER=2009" & set "VID=20H2" & set "CB=19042.631.201119-0144.20h2_release_svc_refresh" & set "CD=2020/11/" & set "CC=1.4.1"
set "CAB=%\\%download.microsoft.com/download/4/3/0/430e9adb-cf08-4b68-9032-eafca8378d42/products_20201119.cab"
set "EXE=%\\%download.microsoft.com/download/4/c/c/4cc6c15c-75a5-4d1b-a3fe-140a5e09c9ff/MediaCreationTool20H2.exe"
:: refreshed 19041 base with integrated 20H2 enablement package to mainly bundle ChrEdge
goto process
:choice-10
set "VER=2004" & set "VID=20H1" & set "CB=19041.508.200907-0256.vb_release_svc_refresh" & set "CD=2020/09/" & set "CC=1.4"
if %INSERT_BUSINESS%0 gtr 1 set "CB=19041.572.201009-1946.vb_release_svc_refresh" & set "CD=2020/11/"
set "CAB=%\\%download.microsoft.com/download/7/4/4/744ccd60-3203-4eea-bfa2-4d04e18a1552/products.cab"
set "EXE=%\\%software-download.microsoft.com/download/pr/8d71966f-05fd-4d64-900b-f49135257fa5/MediaCreationTool2004.exe"
:: visible improvements to windows update, defender, search, dx12, wsl, sandbox
goto process
:choice-9
set "VER=1909" & set "VID=19H2" & set "CB=18363.592.200109-2016.19h2_release_svc_refresh" & set "CD=2020/01/" & set "CC=1.3"
if %INSERT_BUSINESS%0 gtr 1 set "CB=18363.1139.201008-0514.19h2_release_svc_refresh" & set "CD=2020/11/"
set "CAB=%\\%download.microsoft.com/download/8/2/b/82b12fa5-cab6-4d37-8167-16630c6151eb/products_20200116.cab"
set "EXE=%\\%download.microsoft.com/download/c/0/b/c0b2b254-54f1-42de-bfe5-82effe499ee0/MediaCreationTool1909.exe"
:: refreshed 18362 base with integrated 19H2 enablement package to activate usability and security fixes
goto process
:choice-8
set "VER=1903" & set "VID=19H1" & set "CB=18362.356.190909-1636.19h1_release_svc_refresh" & set "CD=2019/09/" & set "CC=1.3"
set "CAB=%\\%download.microsoft.com/download/4/e/4/4e491657-24c8-4b7d-a8c2-b7e4d28670db/products_20190912.cab"
set "EXE=%\\%download.microsoft.com/download/9/8/8/9886d5ac-8d7c-4570-a3af-e887ce89cf65/MediaCreationTool1903.exe"
:: modern windows 10 starts here with proper memory allocation, cpu scheduling, security features
goto process
:choice-7
set "VER=1809" & set "VID=1809" & set "CB=17763.379.190312-0539.rs5_release_svc_refresh" & set "CD=2019/03/" & set "CC=1.3"
set "CAB=%\\%download.microsoft.com/download/8/E/8/8E852CBF-0BCC-454E-BDF5-60443569617C/products_20190314.cab"
set "EXE=%\\%software-download.microsoft.com/download/pr/MediaCreationTool1809.exe"
:: rather mediocre considering it is the base for ltsc 2019; less smooth than 1803 in games; intel pre-4th-gen still buggy
goto process
:choice-6
set "VER=1803" & set "VID=1803" & set "CB=17134.112.180619-1212.rs4_release_svc_refresh" & set "CD=2018/07/" & set "CC=1.2"
set "CAB=%\\%download.microsoft.com/download/5/C/B/5CB83D2A-2D7E-4129-9AFE-353F8459AA8B/products_20180705.cab"
set "EXE=%\\%software-download.microsoft.com/download/pr/MediaCreationTool1803.exe"
:: update available to finally fix most standby memory issues that were present since 1703; intel pre-4th-gen still buggy
goto process
:choice-5
set "VER=1709" & set "VID=1709" & set "CB=16299.125.171213-1220.rs3_release_svc_refresh" & set "CD=2018/01/" & set "CC=1.1"
set "CAB=%\\%download.microsoft.com/download/3/2/3/323D0F94-95D2-47DE-BB83-1D4AC3331190/products_20180105.cab"
set "EXE=%\\%download.microsoft.com/download/A/B/E/ABEE70FE-7DE8-472A-8893-5F69947DE0B1/MediaCreationTool.exe"
:: plagued by standby and other memory allocation bugs, fullscreen optimisation issues, worst version of windows 10 by far
goto process
:choice-4
set "VER=1703" & set "VID=1703" & set "CB=15063.0.170317-1834.rs2_release" & set "CD=2017/03/" & set "CC=1.0"
if %INSERT_BUSINESS%0 gtr 1 set "CB=15063.0.170710-1358.rs2_release_svc_refresh" & set "CD=2017/07/"
rem set "XML=%\\%download.microsoft.com/download/2/E/B/2EBE3F9E-46F6-4DB8-9C84-659F7CCEDED1/products20170727.xml"
rem above refreshed xml often fails decrypting dual x86 + x64 - using rtm instead; the added enterprise + cloud are refreshed
set "CAB=%\\%download.microsoft.com/download/9/5/4/954415FD-D9D7-4E1F-8161-41B3A4E03D5E/products_20170317.cab"
set "EXE=%\\%download.microsoft.com/download/1/F/E/1FE453BE-89E0-4B6D-8FF8-35B8FA35EC3F/MediaCreationTool.exe"
:: some gamers still find it the best despite unfixed memory allocation bugs and exposed cpu flaws; can select Cloud (S)
goto process
:choice-3
set "VER=1607" & set "VID=1607" & set "CB=14393.0.161119-1705.rs1_refresh" & set "CD=2017/01/" & set "CC=1.0"
set "CAB=%\\%wscont.apps.microsoft.com/winstore/OSUpgradeNotification/MediaCreationTool/prod/Products_20170116.cab"
set "EXE=%\\%download.microsoft.com/download/C/F/9/CF9862F9-3D22-4811-99E7-68CE3327DAE6/MediaCreationTool.exe"
:: snappy and stable for legacy hardware
goto process
:choice-2
set "VER=1511" & set "VID=1511" & set "CB=10586.0.160426-1409.th2_refresh" & set "CD=2016/05/" & set "CC=1.0"
set "XML=%\\%wscont.apps.microsoft.com/winstore/OSUpgradeNotification/MediaCreationTool/prod/Products05242016.xml"
set "EXE=%\\%download.microsoft.com/download/1/C/4/1C41BC6B-F8AB-403B-B04E-C96ED6047488/MediaCreationTool.exe"
rem 1511 MCT exe works and can select Education - using 1607 one instead anyway for unified products.xml catalog 1.0 format
set "EXE=%\\%download.microsoft.com/download/C/F/9/CF9862F9-3D22-4811-99E7-68CE3327DAE6/MediaCreationTool.exe"
:: most would rather go with 1507 or 1607 instead as with a little effort can apply latest ltsb updates on all editions
goto process
:choice-1
set "VER=1507" & set "VID=1507" & set "CB=10240.16393.150909-1450.th1_refresh" & set "CD=2015/09/" & set "CC=1.0"
set "XML=%\\%wscont.apps.microsoft.com/winstore/OSUpgradeNotification/MediaCreationTool/prod/Products09232015_2.xml"
set "EXE=%\\%download.microsoft.com/download/1/C/8/1C8BAF5C-9B7E-44FB-A90A-F58590B5DF7B/v2.0/MediaCreationToolx64.exe"
set "EXE32=%\\%download.microsoft.com/download/1/C/8/1C8BAF5C-9B7E-44FB-A90A-F58590B5DF7B/v2.0/MediaCreationTool.exe"
if /i "%PROCESSOR_ARCHITECTURE%" equ "x86" if not defined PROCESSOR_ARCHITEW6432 set "EXE=%EXE32%"
rem 1507 MCT exe works but cant select Education - using 1607 one instead anyway for unified products.xml catalog 1.0 format
set "EXE=%\\%download.microsoft.com/download/C/F/9/CF9862F9-3D22-4811-99E7-68CE3327DAE6/MediaCreationTool.exe"
:: fastest for potato PCs
goto process
:choice-
:choice-0
%<%:e1 " CANCELED "%>% & popd & timeout /t 5 >nul & EXIT/B
:start
@echo off & title %~nx0 & color 1f & mode 120,30 & set "__COMPAT_LAYER=Installer"
:: self-echo top 1-20 lines of script
<"%~f0" (set/p \=&for /l %%/ in (1,1,22) do set \=& set/p \=& call echo;%%\%%)
:: lean xp+ color macros by AveYo: %<%:af " hello "%>>% & %<%:cf " w\"or\"ld "%>% for single \ / " use .%|%\ .%|%/ \"%|%\"
for /f "delims=:" %%\ in ('echo;prompt $h$s$h:^|cmd/d') do set "|=%%\" &set ">>=\..\c nul &set/p \=%%\%%\%%\%%\%%\%%\%%\<nul&popd"
set "<=pushd "%allusersprofile%"&2>nul findstr /c:\ /a" &set ">=%>>%&echo;" &set "|=%|:~0,1%" &set/p \=\<nul>"%allusersprofile%\c"
:: generate a latest_MCT_script.url file for manual update - could have made the script to update itself, but decided against it
for %%s in (latest_MCT_script.url) do if not exist %%s (echo;[InternetShortcut]&echo;URL=git.io/MediaCreationTool.bat)>%%s
:: baffling pastebin url filters..
for %%s in (tp://) do set "\\=ht%%s"
:: (un)define main variables
for %%v in (NO_UPDATE NO_UNDO NO_OEM ACT AUTO CREATE ISO DEFAULT EDITION KEY ARCH LANGCODE VID MCT XML CAB EXE) do set "%%v="
set OPTIONS=/Selfhost& exit/b
:process
:: parse gui / cmdline action
if %ACT% equ 1 set "ACTION=Auto Setup" & set "AUTO=1" & set "ISO=" & set "CREATE="
if %ACT% equ 2 set "ACTION=Create ISO" & set "AUTO=" & set "ISO=1" & set "CREATE=1"
if %ACT% equ 3 set "ACTION=Select in MCT" & for %%v in (CREATE AUTO ISO EDITION LANGCODE ARCH KEY) do set "%%v="
:: cleanup MCT workfolder / temporary ESD
pushd "%~dp0" & mkdir MCT >nul 2>nul & pushd MCT
del /f /q products.* 2>nul & set/a latest=0 & if exist latest set/p latest=<latest
echo,20210621>latest & if %latest% lss 20210531 del /f /q products*.* MediaCreationTool*.exe 2>nul
:: pre-run test of the built-in :DIR2ISO lean and mean snippet to automate iso creation, or fallback to manual selection in MCT
if defined CREATE (mkdir test\iso & copy /y "%~f0" test\iso\tmp.txt & call :DIR2ISO "test" "test.iso") >nul 2>nul
if defined CREATE for %%/ in (test.iso) do (rd/s/q test & del/f/q test.iso & if %%~z/0 lss 300000 set "CREATE=") >nul 2>nul
set FREE=0& for /f %%s in ('powershell -nop -c "[Math]::Round((Get-PSDrive $pwd.Path[0]).Free/1073741824)"') do set "FREE=%%~s"
if defined CREATE if %FREE% LSS 5 set CREATE=& %<%:1e "INFO: Run from a drive with 5GB+ free to create iso directly "%>% &echo;
if defined ISO if not defined CREATE set/a ACT=3 & set "ACTION=Select in MCT"
:: initial detected / selected media preset
if defined EDITION (set MEDIA_EDITION=%EDITION%) else (set MEDIA_EDITION=%OS_EDITION%)
if defined LANGCODE (set MEDIA_LANGCODE=%LANGCODE%) else (set MEDIA_LANGCODE=%OS_LANGCODE%)
if defined ARCH (set MEDIA_ARCH=%ARCH%) else (set MEDIA_ARCH=%OS_ARCH%)
:: edition fallback per MCT support and script choices - finally corrected
(set MEDIA_EDITION=%MEDIA_EDITION:Embedded=Enterprise%)
(set MEDIA_EDITION=%MEDIA_EDITION:EnterpriseS=Enterprise%)
if defined CREATE (set MEDIA_EDITION=%MEDIA_EDITION:ProfessionalWorkstation=Enterprise%)
if defined CREATE (set MEDIA_EDITION=%MEDIA_EDITION:ProfessionalEducation=Education%)
if %VER% leq 1709 (set MEDIA_EDITION=%MEDIA_EDITION:ProfessionalWorkstation=Enterprise%)
if %VER% leq 1709 (set MEDIA_EDITION=%MEDIA_EDITION:ProfessionalEducation=Education%)
if %VER% leq 1511 (set MEDIA_EDITION=%MEDIA_EDITION:Enterprise=Professional%)
if %VER% leq 1703 if %INSERT_BUSINESS%0 lss 1 (set MEDIA_EDITION=%MEDIA_EDITION:Enterprise=Professional%)
if %VER% leq 1511 if %UNHIDE_BUSINESS%0 lss 1 (set MEDIA_EDITION=%MEDIA_EDITION:Education=Professional%)
if not defined EDITION if "%MEDIA_EDITION%" neq "%OS_EDITION%" set "EDITION=%MEDIA_EDITION%"
:: generic key preset - only staged editions in MCT install.esd - see sources\product.ini
for %%/ in (%MEDIA_EDITION%) do for %%K in (
YTMG3-N6DKC-DKB77-7M9GH-8HVX7.Core 4CPRK-NM3K3-X6XXQ-RXX86-WXCHW.CoreN
BT79Q-G7N6G-PGBYW-4YWX6-6F4BT.CoreSingleLanguage N2434-X9D7W-8PF6X-8DV9T-8TYMD.CoreCountrySpecific
VK7JG-NPHTM-C97JM-9MPGT-3V66T.Professional 2B87N-8KFHP-DKV6R-Y2C8J-PKCKT.ProfessionalN
8PTT6-RNW4C-6V7J2-C2D3X-MHBPB.ProfessionalEducation GJTYN-HDMQY-FRR76-HVGC7-QPF8P.ProfessionalEducationN
DXG7C-N36C4-C4HTG-X4T3X-2YV77.ProfessionalWorkstation WYPNQ-8C467-V2W6J-TX4WX-WT2RQ.ProfessionalWorkstationN
YNMGQ-8RYV3-4PGQ3-C8XTP-7CFBY.Education 84NGF-MHBT6-FXBX8-QWJK7-DRR8H.EducationN
NPPR9-FWDCX-D2C8J-H872K-2YT43.Enterprise DPH2V-TTNVB-4X9Q3-TJR4H-KHJW4.EnterpriseN
) do if /i %%~xK equ .%%/ set MEDIA_EDITION=%%~xK& call set MEDIA_EDITION=%%MEDIA_EDITION:.=%%& set "MEDIA_KEY=%%~nK"
:: detected / selected media preset
set MEDIA=& for %%s in (%EDITION%%LANGCODE%%ARCH%%KEY%) do (set MEDIA=%%s)
if defined MEDIA for %%s in (%MEDIA_EDITION%) do (set EDITION=%%s)
if defined MEDIA for %%s in (%MEDIA_LANGCODE%) do (set LANGCODE=%%s)
if defined MEDIA for %%s in (%MEDIA_ARCH%) do (set ARCH=%%s)
if defined MEDIA for %%s in (%MEDIA_KEY%) do (if not defined KEY set KEY=%MEDIA_KEY%)
:: show media preset label
%<%:f0 " Windows 10 Version "%>>% & %<%:2f " %VID% "%>>% & %<%:f1 " %CB% "%>>%
%<%:8f " %MEDIA_LANGCODE% "%>>% & %<%:3f " %MEDIA_EDITION% "%>>% & %<%:5f " %MEDIA_ARCH% "%>>%
%<%:11 ~%>% & echo;
:: download MCT and CAB / XML
set "DOWN=function dl($u,$f){$w=new-object Net.WebClient; $w.Headers.Add('user-agent','ipad'); try{$w.DownloadFile($u,$f)}"
set "LOAD=catch [Net.WebException] {write-host -non ';('; del $f -force -ea 0} finally{$w.Dispose()} } ; dl"
if defined EXE echo;%EXE%
if not exist MediaCreationTool%VER%.exe powershell -nop -c "%DOWN% %LOAD% $env:EXE 'MediaCreationTool%VER%.exe'" 2>nul
if not exist MediaCreationTool%VER%.exe %<%:1e " MediaCreationTool%VER%.exe download failed "%>%
if defined XML echo;%XML%
if defined XML if not exist products%VER%.xml powershell -nop -c "%DOWN% %LOAD% $env:XML 'products%VER%.xml'" 2>nul
if defined XML if not exist products%VER%.xml %<%:1e " products%VER%.xml download failed "%>%
if defined XML if exist products%VER%.xml copy /y products%VER%.xml products.xml >nul 2>nul
if defined CAB echo;%CAB%
if defined CAB if not exist products%VER%.cab powershell -nop -c "%DOWN% %LOAD% $env:CAB 'products%VER%.cab'" 2>nul
if defined CAB if not exist products%VER%.cab %<%:1e " products%VER%.cab download failed "%>%
if exist products%VER%.cab del /f /q products%VER%.xml 2>nul
if exist products%VER%.cab expand.exe -R products%VER%.cab -F:* . >nul 2>nul
echo; & set "err=" & for %%s in (products.xml MediaCreationTool%VER%.exe) do if not exist %%s set err=1
if defined err %<%:4f " ERROR "%>>% & %<%:0f " Check urls in browser | del MCT dir | unblock powershell | enable BITS serv "%>%
if defined err del /f /q products%VER%.* MediaCreationTool%VER%.exe 2>nul & pause & EXIT/B
if not defined err %<%:0f " %ACTION% "%>% . . .
:: configure products.xml in one go via powershell snippet
call :PRODUCTS_XML
:: repack XML into CAB
makecab products.xml products.cab >nul
:: pause couple seconds before launching MCT directly / via powershell runas admin
timeout /t 5 >nul
:: ===================================================================================================
:: "Auto Setup" : after MCT authors sources, script starts auto.cmd setup (setupprep with OPTIONS)
:: "Create ISO" : after MCT authors sources, script creates iso via built-in :DIR2ISO snippet
:: "Select in MCT" : script just provides products.xml and OPTIONS to MCT for processing, then quits
:: ===================================================================================================
if "Select in MCT" equ "%ACTION%" (start MediaCreationTool%VER%.exe %OPTIONS%& EXIT/B)
set OPTIONS=%OPTIONS% /Eula Accept /SkipSummary& set "LABEL=%VID%" & set "DIR=%SystemDrive%\ESD\Windows"
for %%s in (%EDITION% %LANGCODE% %ARCH%) do call set "LABEL=%%LABEL%% %%s"
(if %OS_VERSION% geq 2600 set "UNDO=") & (if %OS_VERSION% lss 1709 set "UNDO=")
set ACTION=/Action CreateUpgradeMedia %UPDATE% %UNDO% /MediaPath %SystemDrive%\ESD\Windows
:: not using /MediaEdition setup option in MCT version 1703 and older - handled via products.xml
if defined MEDIA for %%s in (%EDITION%) do if %VER% gtr 1703 (set OPTIONS=%OPTIONS% /MediaEdition %%s)
if defined MEDIA for %%s in (%LANGCODE%) do if %VER% gtr 1703 (set OPTIONS=%OPTIONS% /MediaLangCode %%s)
if defined MEDIA for %%s in (%ARCH%) do if %VER% gtr 1703 (set OPTIONS=%OPTIONS% /MediaArch %%s)
:: generate sources\PID.txt to preset EDITION on boot media - MCT install.esd indexes only, ProWS/ProEdu auto upgrade ONLY
if not defined AUTO for %%/ in (Workstation WorkstationN Education EducationN) do if "Professional%%/" equ "%EDITION%" set "KEY="
if not defined KEY (del /f /q PID.txt 2>nul) else (echo;[PID]& echo;Value=%KEY%& echo;;Edition=%EDITION%) >PID.txt
:: generate auto.cmd for auto upgrade 2nd stage - more reliably pass OPTIONS via setupprep and workaround to keep files and apps
>auto.cmd echo;@echo off ^& title MediaCreationTool.bat: auto upgrade Windows 10 directly with suitable options
>>auto.cmd echo;
>>auto.cmd echo;set "MediaBuild=%VER%" ^& set "MediaEdition=%EDITION%" ^& set "dir=" ^& pushd "%%~dp0"
>>auto.cmd echo;for %%%%i in ("x86\" "x64\" "") do if exist "%%%%~isources\setupprep.exe" set "dir=%%%%~i"
>>auto.cmd echo;pushd "%%dir%%sources" ^|^| (pause ^& exit/b)
>>auto.cmd echo;set MediaEdition ^|^| goto upgrade
>>auto.cmd echo;
>>auto.cmd echo;:: cross-edition in-place / upgrade / downgrade check
>>auto.cmd echo;set CV64="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
>>auto.cmd echo;set CV32="HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion"
>>auto.cmd echo;for /f "tokens=2*" %%%%R in ('reg query %%CV64%% /v EditionID 2^^^>nul') do set "EditionID=%%%%S"
>>auto.cmd echo;for /f "tokens=2*" %%%%R in ('reg query %%CV64%% /v ProductName 2^^^>nul') do set "ProductName=%%%%S"
>>auto.cmd echo;for /f "tokens=2*" %%%%R in ('reg query %%CV64%% /v CurrentBuildNumber 2^^^>nul') do set "Build=%%%%S"
>>auto.cmd echo;if /i "%%EditionID%%" equ "%%MediaEdition%%" goto upgrade
>>auto.cmd echo;if /i "%%EditionID%%" neq "Embedded" if "%%Build%%" gtr "1909" if "%%MediaBuild%%" gtr "1909" goto upgrade
>>auto.cmd echo;if /i "%%EditionID%%" neq "Embedded" if "%%Build%%" gtr "2600" goto upgrade
>>auto.cmd echo;if /i "%%EditionID%%" equ "Embedded" set "MediaEdition=Enterprise"
>>auto.cmd echo;fltmc^>nul ^|^| (echo EditionID=%%EditionID%% [MISMATCH] - Run as administrator to bypass ^& timeout /t 10)
>>auto.cmd echo;
>>auto.cmd echo;:: cross-edition in-place / upgrade workaround to keep files and apps - 20H1/20H2/21H1 also supports downgrade
>>auto.cmd echo;(reg query %%CV64%% /v ProductName_UNDO ^|^| reg add %%CV64%% /v ProductName_UNDO /d "%%ProductName%%" /f
>>auto.cmd echo; reg query %%CV64%% /v EditionID_UNDO ^|^| reg add %%CV64%% /v EditionID_UNDO /d "%%EditionID%%" /f
>>auto.cmd echo; reg add %%CV64%% /v EditionID /d "%%MediaEdition%%" /f ^& reg delete %%CV64%% /v ProductName /f
>>auto.cmd echo; reg query %%CV32%% /v EditionID ^&^& (
>>auto.cmd echo; reg query %%CV32%% /v ProductName_UNDO ^|^| reg add %%CV32%% /v ProductName_UNDO /d "%%ProductName%%" /f
>>auto.cmd echo; reg query %%CV32%% /v EditionID_UNDO ^|^| reg add %%CV32%% /v EditionID_UNDO /d "%%EditionID%%" /f
>>auto.cmd echo; reg add %%CV32%% /v EditionID /d "%%MediaEdition%%" /f ^& reg delete %%CV32%% /v ProductName /f
>>auto.cmd echo;)) ^>nul 2^>nul
>>auto.cmd echo;
>>auto.cmd echo;:upgrade
>>auto.cmd echo;start "auto" setupprep.exe %OPTIONS% %UPDATE% %UNDO% /MigChoice Upgrade /Auto Upgrade
:: run MCT /Action CreateUpgradeMedia with edition, then include auto.cmd / PID.txt / $OEM$ and start auto.cmd (if needed)
set "mct=%__cd__:'=''%mct.ps1"
>mct.ps1 echo;cd -Lit(split-path '%mct%'); $AUTO='%AUTO%'-ne''; $OEM='%NO_OEM%'-eq''
>>mct.ps1 echo;$DIR='%DIR:'=''%'; cmd "/c del /f/q $DIR & rd /s/q $DIR"
>>mct.ps1 echo;
>>mct.ps1 echo;$CV64='HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'
>>mct.ps1 echo;$CV32='HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion'
>>mct.ps1 echo;if (%VER% -le 1703 -and '%EDITION%'-ne'') {
>>mct.ps1 echo; sp $CV64 EditionID "%EDITION%" -force; rp $CV64 ProductName -force
>>mct.ps1 echo; if (test-path $CV32) {sp $CV32 EditionID "%EDITION%" -force; rp $CV32 ProductName -force}
>>mct.ps1 echo;}
>>mct.ps1 echo;
>>mct.ps1 echo;$MCT=start -wait MediaCreationTool%VER%.exe '%OPTIONS:'=''% %ACTION:'=''%'
>>mct.ps1 echo;
>>mct.ps1 echo;"$DIR\x86\sources","$DIR\x64\sources","$DIR\sources" ^|%% {
>>mct.ps1 echo; if (Test-Path "$_\setupprep.exe") {
>>mct.ps1 echo; if (Test-Path "PID.txt") {copy -Path "PID.txt" -Dest $_ -force}
>>mct.ps1 echo; if ($OEM -and (Test-Path '..\$OEM