Create Virtual Machines Quickly using MDGHOST - Part 1

Create Virtual Machines Quickly using MDGHOST - Part 1

  • Comments 4
  • Likes

MDGHOST builds differencing disks based on parent drives.  In a test lab I find this useful because I usually have sysprepped parent drives for each OS ready to go.  the script will build VM's very quickly, without the need for SCVMM.  I find this useful in single machine scenarios, and I’m spoiled on it, because it’s DANG FAST.  Mdghost requires powershell be run as administrator (much of the Hyper-V cmdlets need admin access to function properly.)

Note that in the sample below, I set my execution policies to unrestricted.  This may not be copacetic in your corporate environment, so please evaluate the trustworthiness of any script (including mine) and be a good citizen of your corporate network environment.  The script is primarily intended to build test labs. 

The script works on 2012, and Windows 8 Hyper-V. 

How to use the script


Edit the script to change your defaults:


$ParentDef ="w2k8r2datacenterbase" <-Default Parent Drive

$Namedef ="Scratch" <- Default Prepended Name for Scratch VHDS

$Networkdef =
"Scratchnet" <- Default Network Name

[int64]$Memdef =1024MB <- Default RAM settings

$Procdef =2 <- Default Number of processors

$ParentDir = "c:\ParentDrives" <- Location of Parent Drives

$global:VHD="v:\VirtualDisks\" <- Where the system will place differencing disks

$procval=1,2,4 <-Acceptable Processor Values

[int64]$maxmem=4192MB <-Maxmem defaults




PSC:\hvml> . .\MDGHOST.ps1

. : File
C:\hvml\MDGHOST.ps1 cannot be loaded because running scripts is disabled on
this system. For more information, see about_Execution_Policies

At line:1 char:3

+ .


+ CategoryInfo          :SecurityError: (:) [], PSSecurityException

+ FullyQualifiedErrorId : UnauthorizedAccess


PS C:\hvml> Set-ExecutionPolicy unrestricted


PS C:\hvml> Get-ExecutionPolicy



PS C:\hvml> . .\MDGHOST.ps1

 *Note that the script must be dot sourced.  That's a . then a space, then a path to the script file.  This is a common mistake.  If you type mdghost -help and don't get anything, it's the most likely cause.

PS C:\hvml> mdghost -help

[parent] [newvhd] [net] [mem] [cpu] [help] [go]


At this point you can use MDGHOST to create VM’s VERY, VERY QUICKLY.  You can build machines as fast as your hardware will allow.  Just typing mdghost will prompt you for all the defaults, with a randomly generated VHD Name (useful if you don’t care, or will rename them later). 


C:\hvml> mdghost

VHD:Scratch-18AEDCC9 Net:10.0.x.y  Memory:1024MB CPU:2

(y/n): y

 The –go swtich allows you to skip the prompt.  If the –parent and –net values passed are ambiguous, the script will prompt you with a numbered list.


VHD:Scratch-1ADFCFDD Net:10.0.x.y  Memory:1024MB CPU:2


PS C:\hvml>
mdghost -parent r2


ID Parent


0 w2k8r2datacenterbase.vhd

1 w2k8r2scSysprep.vhd    

 Which one?: 


Attachment: MDGHOST.ps1
  • That's really usefull to deploy new VMs quickly. Great article!

  • FWIW - Found in a hyper-v demo that this is not working with vhdx parents.  Working on a new rev.  The people that have come to me for help with the script are generally running into two problems:

    1.  Defaults not defined properly

    2.  Not dot sourcing the script

    Parent Drives for the moment must be .vhd, not .vhdx.  New Version will include support for dynamic memory.

    Planned enhancements for next version include writing defaults to the registry, and prompting for creation of default switch.

  • If you've created a VM with VHDX you can convert it to VHD as described in the following post

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment