Skip to content

Instantly share code, notes, and snippets.

@u1735067
Last active June 14, 2021 12:52
Show Gist options
  • Save u1735067/0d742138b978c28a1695 to your computer and use it in GitHub Desktop.
Save u1735067/0d742138b978c28a1695 to your computer and use it in GitHub Desktop.
Windows All-In-One bootable USB key maker script
@echo off
:: MkBootUSB
:: @author: u1735067
:: @date: 2015-07-30
:: @url: https://gist.github.com/Alex131089/0d742138b978c28a1695
setlocal enableextensions
set debug=0
:: Print some informations
echo ################################################################################
echo # Windows All-In-One bootable USB key maker script #
echo # ------------------------------------------------ #
echo # The AIO ISO is made with an unique install.wim containing all editions^&arch, #
echo # copied but deduplicated in the ISO (hardlinked for both architectures), so #
echo # this script will extract only one install.wim and recreate the hardlink for #
echo # the USB Key NTFS FS. #
echo # Dual architecture ISO structure taken from the official MediaCreationTool. #
echo ################################################################################
echo\
:: Starting script
:: Source drive
set sdrive=%~d0%
:: Construct USB volume list to get d(est)drive
:: http://stackoverflow.com/questions/10147302/batch-file-and-variable-usb-drive-letters
:: http://stackoverflow.com/questions/17605767/create-list-or-arrays-in-windows-batch
:: wmic logicaldisk where "DeviceID='F:'" get volumename,deviceid,caption,name 2>NUL |findstr F:
set usblist=
for /F "usebackq tokens=1" %%i in (`wmic logicaldisk where "drivetype=2" get caption 2^>NUL ^| more +1 ^| findstr ":"`) do (
set "usblist=%usblist% %%i"
)
echo ----Available drives :
wmic logicaldisk where "drivetype=2" get caption,description,volumename 2>NUL
if %debug% == 1 echo usblist=%usblist%
if "%usblist%" == "" (
echo ^> No USB Key available :(
goto aborted
)
:: Choose the destination volume (ddrive)
set ask=
if /i not "%1%" == "" (
set "ask=%1%"
) else (
set /p ask="What is the destination drive letter (type unlisted letter to abort) ? "
)
set "input_drive=%ask:~0,1%:"
set valid=0
for /F "delims= " %%d in ("%usblist%") do (
if /i "%%d" == "%input_drive%" (
set valid=1
set ddrive=%%d
)
)
if %valid% == 1 (
echo ^> Drive %ddrive% accepted
) else (
echo ^> Drive "%input_drive%" not accepted
goto aborted
)
echo\
:: Diskpart : create script file, ask, apply
set ask=
echo --Making partition active
set "scriptfile=%temp%\mkbootusb.txt"
echo select volume=%ddrive% > "%scriptfile%"
echo clean >> "%scriptfile%"
echo convert mbr >> "%scriptfile%"
echo create partition primary >> "%scriptfile%"
echo format fs=ntfs quick label="Windows 10 AIO" >> "%scriptfile%"
echo active >> "%scriptfile%"
echo exit >> "%scriptfile%"
type "%scriptfile%"
echo\
set /p ask="This diskpart script will be executed, the device will be zeroed, continue [y/N] ? "
if /i not "%ask:~0,1%" == "y" goto aborted
diskpart /s "%scriptfile%"
if %ERRORLEVEL% == 0 (
echo ^> Ok, device ready to boot
) else (
echo ^> Call to diskpart failed
goto aborted
)
:: Skip the bootsect part, useless according to tests, diskpart is installing a boot sector
goto copy
echo\
:: Bootsect : install bootloader
set ask=
echo --Installing boot sector
set /p ask="Install bootsector (optional but required for BIOS based computer, require launching this script with admin rights) [y/n/Abort] ? "
if /i not "%ask:~0,1%" == "y" (
if /i not "%ask:~0,1%" == "n" (
goto aborted
) else (
goto copy
)
)
bootsect /nt60 %ddrive% /mbr
if %ERRORLEVEL% == 0 (
echo ^> Bootsector installed
) else (
echo ^> Call to bootsect failed, will try to continue anyway
goto copy
)
:copy
echo\
:: Copy all files from the ISO except the duplicated install.wim
echo --Copying files
echo x64\sources\install.wim > "%temp%\xclude.txt"
echo Copying files (it might take a while) ...
XCOPY %sdrive% %ddrive% /E /H /Y /K /EXCLUDE:%temp%\xclude.txt >NUL
if %ERRORLEVEL% == 0 (
echo ^> Files copied
) else (
echo ^> Error copying files
goto aborted
)
echo\
:: Hardlink install.wim to recreate the duplicate so each arch will be able to find it
echo --Creating hardlink
mklink /H %ddrive%\x64\sources\install.wim %ddrive%\x86\sources\install.wim
if not %ERRORLEVEL% == 0 (
echo ^> Error creating hardlink
goto aborted
)
:: When finished
echo\
echo :-) Finished, enjoy !
echo\
pause
goto :eof
:: Annulé
:aborted
echo\
echo /!\ Aborted
pause
goto :eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment