Scripting Windows installation


On my physical devices, I only use Windows 10 Pro edition as the primary, physical OS. All other secondary operating systems will then be installed on a native boot VHD instead, for that I have nice PowerShell scripts. I needed USB install media to first wipe and then partition a physical GPT disk, either the primary or secondary disk, and depending on if the device has only one disk, or is a dual disk system, Windows will be installed to the primary or secondary disk I choose,. Finally I deploy the Pro edition on selected disk.

I thought that I’d share this project and the scripts required. Please notice that because these scripts are for my personal use, they are quite “stripped down”. Feel free to add elements such as edition selection, error handling and more.

To get what I want, I need three different DISKPART scripts, a batch file to run them, and an unattended answer file to automate OOBE after deployment. All scripts, the answer file and batch file can be viewed on my OneDrive. I’ve added remarks to scripts and batch file Deploy.cmd to explain them. The answer file unattend.xml is for my personal use, to install an EN-GB Windows 10 version with a Finnish keyboard layout, and create a single local admin account Kari. Edit the answer file to meet your own specific requirements, please.

After writing these scripts, I downloaded the ISO I needed (Win 10 Pro, latest dev channel Insider) from UUP Dump. I mounted that ISO, and copied its contents to folder ISO_Files on my D: drive.

The next step is to mount the boot.wim file (with index 2) to an empty folder C:\Mount for offline servicing using the following command in an elevated PowerShell:

Mount-WindowsImage -ImagePath D:\ISO_Files\Sources\boot.wim -Index 2 -Path C:\Mount

When that’s done, it’s time to open folder C\Mount, and create a folder Scripts. Copy the DISKPART scripts SingleDisk.txt, DualDisk1.txt ,DualDisk2.txt and answer file unattend.xml to that folder (C:\Mount\Scripts):

Copy the batch file Deploy.cmd to the C:\Mount\Sources folder.

Close File Explorer. Now we can save changes to boot.wim using the following command in elevated PowerShell:

Dismount-WindowsImage -Path C:\Mount -Save

Now, the only thing missing is to create USB install media using the contents of the D:\ISO_Files. In fact, you can simply copy the contents of the folder to a FAT32-formatted USB. Rename the USB flash drive to W10USB, because the deployment batch file is looking for a USB with that exact name.

That’s it. You now have a totally automated set of install media. When Windows boots to the region selection screen, press SHIFT + F10 to open Command Prompt. At this point, the boot.wim will be extracted, all its contents copied to RAM disk X, including all scripts, the answer file and the batch file we copied to the mounted WIM file. Run the batch file using this simple command: Deploy. The process starts, showing a menu:

Everything happens without any user interaction, except disk selection, and finally you can see this:

Disk or disks are partitioned correctly, Windows is deployed without any user interaction, ready for the user to boot to the desktop for the first time. Next time a new build is released, you just need to replace the install.wim on USB drive’s Sources folder with the one of the new build.

I would be very interested to hear your ideas on how to make these scripts better. Please comment below. Last but not least, here’s the deployment batch file Deploy.cmd to show how it was built. Click Expand source to view it.

Kari

echo off
cls
rem
rem Create menu
rem
:Menu
echo.
echo #######################################
echo #    Select disk to deploy Windows.   #
echo #                                     #
echo # Select 1, if deploying Windows on a #
echo # single disk system.                 #
echo #                                     #
echo # Select 2, if deploying Windows to   #
echo # primary disk on dual disk system.   #
echo #                                     #
echo # Select 3, if deploying Windows to   #
echo # secondary disk on dual disk system. #
echo #                                     #
echo # Select 4 to quit.                   #
echo #######################################
echo.
echo 1 - Single disk
echo 2 - Dual disk primary
echo 3 - Dual disk secondary
echo 4 - Exit
echo.
rem
rem Ask user to select an option
rem
set /P M=Press 1, 2, 3, or 4 and Enter:
if %M%==1 goto Single
if %M%==2 goto Dual1
if %M%==3 goto Dual2
if %M%==4 goto Exit
:Single
rem
rem User selected option 1. 
rem
start /wait diskpart /s X:\Scripts\SingleDisk.txt
cls
goto Deploy
:Dual1
rem
rem User selected option 2. 
rem
start /wait diskpart /s X:\Scripts\DualDisk1.txt
cls
goto Deploy
rem
:Dual2
rem
rem User selected option 3.
rem
start /wait diskpart /s X:\Scripts\DualDisk2.txt
cls
goto Deploy
rem
Rem User selected option 4. Script
rem will exit to Windows Setup
rem
:Exit
exit
rem
:Deploy
rem
rem Deploy Windows
rem
echo off
echo.
for /f %%X in ('wmic volume get DriveLetter ^, Label ^| find "W10USB"') do DISM /Apply-Image /ImageFile:%%X\Sources\install.wim /index:1 /ApplyDir:W:\
rem
rem Create boot entry
rem
W:\Windows\System32\bcdboot W:\Windows /s S:
rem
rem Create necessary folders, 
rem copy answer file to Panther folder,
rem copy recovery environment to WinRE partition
rem
md W:\Windows\Panther
md R:\Recovery\WinRE
copy X:\Scripts\unattend.xml W:\Windows\Panther\
xcopy /h W:\Windows\System32\Recovery\Winre.wim R:\Recovery\WinRE\
rem
rem Set recovery image location
rem
W:\Windows\System32\Reagentc /Setreimage /Path R:\Recovery\WinRE /Target W:\Windows
cls
echo.
rem
rem Restart to OOBE
rem
echo Computer will restart to OOBE in a few seconds...
W:\Windows\System32\shutdown -r -t 5
 

Author: Kari Finn

A former Windows Insider MVP, Kari started in computing in the mid 80’s writing code for VAX / VMS systems. Since then, he’s worked in a variety of IT positions. He specializes in Windows image capture, customization, repair and deployment as well as Hyper-V virtualization. Kari is a proud Team Member at number #1 Windows site TenForums.com.