I am a huge fan of using native boot virtual hard disks in a dual or multi boot configuration. Deploying Windows to a VHD file and adding it to the boot menu makes things so easy: when the secondary OS is no longer needed, just delete that VHD and remove its boot entry. In an earlier post, I showed how easy and practical this is: Native boot VHD – Dual boot made easy!
In that story, I described how to deploy a default Windows image to a VHD. However, a recent thread on Ten Forums got me interested in automating the whole process. Take any Windows image in a WIM file, add the correct drivers and optional features such as Hyper-V, Windows Sandbox and WSL, add an answer file to further customize the deployment, then do a “One-Click deployment of Windows” to a VHD, adding it to the boot menu.
In this post, I will show how to do these things. I will provide a complete PowerShell script. If you do not want to read everything, just check the prerequisites below, and run the script.
View and download the script from my OneDrive: DeployVHD.ps1
I do not like scripts that keep asking question after question to get required information. It’s not what I consider a “One-Click” solution. That’s why this process requires some manual effort before the script is run.
First, we need to create a folder to store a Windows 10 WIM image, and an optional answer file . I use folder D:\WIM, but you can create this folder on any drive you like. Copy any Windows WIM image (an install.wim file) and, if you have one, an answer file (unattend.xml) to the folder named WIM. Here’s my normal unattend.xml answer file which completely bybasses OOBE when applied. Feel free to change it to cater your specific needs: unattend.xml.
Another thing required is a free drive letter, to be used as temporary mount letter for a VHD file for deployment. My default is W, but you can change that to any free, not-reserved drive letter. Last but not least, is the size of the VHD file. In my VHD deployments, I usually create 100 GB (102400 MB) virtual hard disks. You can change all three of these variable values in lines 78 to 80 in the script:
Line 78: $WimFolder = 'D:\Wim' Line 79: $VHDLetter = 'W' Line 80: $VHDSize = '102400'
Those variables are the only things you need to change, if those current values do not suit you. Save the script anywhere, right click it, and select Run with PowerShell. That’s it. The script elevates itself automatically.
Please notice that this script reguires the PowerShell Hyper-V module. To get that module, you must install it from the Windows optional features pane in Control Panel/Programs and Features (press WIN + R, type optionalfeatures, press Enter):
Note: You need not install the Hyper-V Platform itself. Hyper-V is not required for the PowerShell module to function.
Run the script
I recommend you view the script on OneDrive, and read all the remarks to see what’s done and how. OneDrive color-codes remarks in green to make them easy to spot:
When running the script, simply follow its on-screen instructions. Windows is deployed to a virtual hard disk file, with all drivers and preferred optional features, an answer file is applied if present, and finally Windows is added to the boot menu. The first boot runs OOBE, automated if you apply an answer file instructed to do so. Because OOBE requires one or two restarts, VHD gets added as the default OS. If you want to make your main physical WIndows installation the default again, press WIN + R and type msconfig, select the Boot tab, select your main OS, and click Set as default:
When you no longer need the VHD, just delete the VHD file and remove its boot entry.
That’s it. If you have any issues or questions, please ask me. Remember: a native boot VHD is the most convenient and easy way to dual boot! This script makes it even easier.
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.