How to Use Differencing Disks to Deploy Multiple VMs Quickly in Hyper-V

Mohammed Chowdhury
Tech Jobs Academy
Published in
4 min readJul 13, 2016
Here are the sizes for the differencing disks I used for our last TJA Project. We ended up saving a lot of space (and time)!

In this post, I’ll be going over how we can use differencing disks to quickly deploy multiple VMs (in Hyper-V) that have the same OS without having to install that OS over and over again. This article assumes the reader has a basic understanding of Windows Server 2012 R2, Powershell, Hyper-V and differencing disks. To learn more about differencing disks, follow this link. I’ll be using Powershell throughout the tutorial because it is usually faster than going through the GUI.

Before we begin, you should know the following PowerShell cmdlet:

get-help cmdletName -showwindow

This will open up a new window with the documentation for any cmdlet. Replace “cmdletName” with the name of the cmdlet you’d like to know more about.

Tools

You’ll need the following tools to follow along this tutorial:

  1. PowerShell
  2. Hyper-V
  3. ISO file of the OS you are trying to install

Creating a Parent Disk with an OS

  1. Using an elevated PowerShell console, create a folder where you will store all your VHDs and VMs. My working directory is E:\Tutorial\ — replace that with your directory in the cmdlets that follow. You can create a folder with the following cmdlet:
ni E:\Tutorial -itemtype Directory

(“ni” is an alias for the “new-item” cmdlet.)

2. Change your directory to the new directory using set-location (or the alias, cd):

cd E:\Tutorial

By doing this, we can refer to “E:\Tutorial” (or whatever your working directory is) by just typing “.\” instead of typing the entire name out every time.

3. Create a new VHD that will be your parent disk:

new-vhd -path .\Win2012R2.vhdx -SizeBytes 120GB -Dynamic

I will be installing Microsoft Windows Server 2012 R2, so I’ve named my VHD “Win2012R2.vhdx,” and given it a size of 120GB. Pick a name and size that makes sense for you. I’ve also made the VHD dynamic so that I will not be using up unnecessary space.

4. Now we’ll be creating a VM to install and setup our initial OS on. I used the following cmdlets:

new-vm -Name TechnicianVM -Path .\ -VHDPath .\Win2012R2.vhdx | ` 
set-vmmemory -DynamicMemoryEnabled $true `
-MaximumBytes 2GB -MinimumBytes 512MB -StartupBytes 1GB

The cmdlets above create a VM called “TechnicianVM” in our working directory, and use the VHD we previously created. I then pipe this cmdlet into the set-vmmemory cmdlet, which allows us to adjust the VM’s memory. I’ve enabled Dynamic Memory, and set the memory range from 512MB to 2GB. The “StartupBytes” switch specifies how much memory the VM should use when it is starting up.

Note: Due to the width constraints of this page, I’ve added a “ ` ” (aka the grave accent) at the end of each line in my code snippet. In PowerShell, the grave accent indicates that the code continues on the next line, so PowerShell will read both lines together before executing.

5. Now we need to add the ISO file that has our OS to the TechnicianVM:

add-vmdvddrive -VMName TechnicianVM -Path E:\WIN2012R2EVAL.ISO

My ISO is located in the root of the E drive. Replace “E:\WIN2012R2EVAL.ISO” with the location of your chosen ISO.

6. Now we can start up the VM and install our OS. That part will not be covered in this tutorial. You can configure any settings you would like duplicated onto your other VMs now. If you are using a Windows based OS, go to step 7. If you are using a non-Windows OS, research the Sysprep equivalent of that OS. Linux has no requirements to Sysprep before deploying multiple copies. You can read more about Sysprep here.

7. If you are using a Windows based OS, start an elevated command prompt, and run the following cmds:

cd %SystemRoot%\System32\Sysprep
sysprep /generalize /oobe /shutdown

Sysprep will strip any unique identifiers from your computer (such as SIDs) and then shutdown the computer. This allows us to make multiple “copies” of the OS using differencing disks. Once the VM shuts down, do not turn it on anymore, because Windows will run some initialization tasks and undo the Sysprep.

Creating the Child Disk(s) and VMs

  1. Now that we have a parent disk with an OS on it, it’s time to create the child disk(s). I’ll cover how to create two child disks, and you can repeat the process as many times as needed. We’ll use the new-vhd cmdlet with some different switches to create two differencing (child) disks:
new-vhd -path .\C1.vhdx -ParentPath .\Win2012R2.vhdx -differencing
new-vhd -path .\C2.vhdx -ParentPath .\Win2012R2.vhdx -differencing

The parent here is the VHD we previously created and installed an OS on. You can create as many child disks as you need for your VMs. I named my differencing disks C1 and C2 — choose names that are appropriate for your situation.

2. Now we can create VMs to attach these differencing disks to:

new-vm -Name VM1 -Path .\ -VHDPath .\C1.vhdx | ` 
set-vmmemory -DynamicMemoryEnabled $true `
-MaximumBytes 2GB -MinimumBytes 512MB -StartupBytes 1GB
new-vm -Name VM2 -Path .\ -VHDPath .\C2.vhdx | `
set-vmmemory -DynamicMemoryEnabled $true `
-MaximumBytes 2GB -MinimumBytes 512MB -StartupBytes 1GB

And voila! You’ve just deployed 2 VMs from a single installation of your OS.

If you like the info in my posts, follow me on Twitter (@MoChowdhuryNYC) where I post links to interesting tidbits I read.

--

--

Mohammed Chowdhury
Tech Jobs Academy

Student. Jack of all trades, master of none. Likes: anything tech, basketball, handball, movies and exploring new hobbies.