In the spirit of sharing I thought I would share how I create virtual machines on Windows 10 using Hyper-v using differencing disks. I work with a lot of different customers and needed a way to quickly create new VM’s ready to go.
A normal approach would be to have a deployment solution (MDT) running on a Virtual machine or a pre-made ISO that automates the installation of a new VM. Or maybe even a script that clones the virtual hard drive and create new VM from that hard drive.
There are many great approaches for creating VM’s, but I prefer this method I am about to describe using differencing disks. Keep in mind that no solution fits all. 🙂
The first thing I do is to create some master images or parent vhds if you prefer. Right now, I have two different ones.
One for Windows 10 1611 and one for Windows Server 2012R2.
The process I use to create these images are very straight forward. You install a VM with Windows 10 1607 from the official ISO, (Next next finish) patch it up, run sysprep and turn it off. The same goes for the server.
I will not cover that in this blogpost. I included a screenshot of the sysprep:
After you have done that I recommend that you copy or move the virtual hard drive of the VM to a folder named for example Hyperv-Masterimages (Hyperv-ParentImages is probably better). Like this:
A recommendation is to write protect the parent vhdx files.
When using differencing disks, you will corrupt all other VM’s based on the parent if you change it, so don’t do that.
The next thing I do is to use my own PowerShell function to generate the VM’s. These functions require administrator privileges, so elevate the PowerShell prompt prior to running it.
The function can be found here:
If you don’t care about downloading it you can run it directly from Powershell using this command:
IEX (New-Object Net.WebClient).DownloadString(‘https://raw.githubusercontent.com/api0cradle/PowershellScripts/master/Hyper-V/New-DifferencingVM.ps1’)
A GIF demonstrating how I use it:
After the VM is created you can fire it up. It only takes a few seconds. And one upside is that it takes a lot less space in terms of storage.
Hope you find this useful.
And when it comes to removing the VM I also use a custom function for that. The function can be found here:
Or run directly from here:
IEX (New-Object Net.WebClient).DownloadString(‘https://raw.githubusercontent.com/api0cradle/PowershellScripts/master/Hyper-V/Remove-VirtualMachine.ps1’)
This function uses dynamic parameters and lists out all vm’s as demonstrated in the GIF below. The function cleans up by removing snapshots, hard drive, removing the VM and folder structure.
If you for some reason need to merge out of using parent child follow guidelines here: https://technet.microsoft.com/en-us/library/cc720304(v=ws.10).aspx
More info about differencing disk here: https://technet.microsoft.com/en-us/library/cc720381(v=ws.10).aspx
And also, I take no responsibility for what you use these scripts for. Test them before putting them into production.