PowerShell Script to Create Lots of Windows Server 2012 Hyper-V Virtual Machines at Once

If you like this solution then you might like a newer script that creates lots of VMs based on specs that are stored in a CSV file.

Here you will find a PowerShell script I just wrote to deploy a lot of Windows Server 8 Hyper-V VMs with the minimum of effort.  I created it because I wanted more load to stress my 20 GbE Live Migration network and creating the VMs by hand was too slow.  Yes, it took me time to figure out and write the script in ISE, but I have it for the future and can lash out a lab in no time now.

Note that this script is using the new cmdlets for Hyper-V (and one cmdlet for clustering) that are in Windows Server 8, and not the VMM cmdlets.

What the script will do:

  1. Create a new folder for each VM on an SMB 2.2 file server shared folder
  2. Create a differencing disk pointing to a parent VHD.  This is for lab purposes only.  You’d do something different like create a new VHDX or copy an existing sysprepped VHDX in production.
  3. Create a new VM (e.g. VM1) using the VHDX
  4. Configure Dynamic Memory
  5. Start the VM
  6. Add the VM to a cluster
  7. It’ll do this 20 times (configurable in the foreach loop).


  • Windows Server 8 SMB file share that is correctly configured
  • A Windows Server 8 Hyper-V cluster
  • A parent VHDX that has been sysprepped.  That will automate the configuration of the VM when it powers up for the first time.

Here’s the script.  My old programmer instinct (which refuses to go away) tells me that it could be a lot cleaner, but this rough and ready script works.  There is also zero error checking which the old programmer instinct hates but this is just for deploying a lab workload.

$parentpath = “\fileserverVirtual Machine 1WinSvr8Beta.vhdx”

$path = “\fileserverVirtual Machine 1”

foreach ($i in 1..20)


#Create the necessary folders

$vmpath = “$pathVM$i”

New-Item -Path $vmpath -ItemType “Directory”

New-Item -Path “$vmpathVirtual Hard Disks” -ItemType “Directory”

#create a VHDX – differencing format

$vhdpath = “$vmpathVitual Hard DisksDisk0.vhdx”

New-VHD -ParentPath $parentpath -Differencing -Path $vhdpath

#Create the VM

New-VM -VHDPath “$vhdpath” -Name “VM$i” -Path “$vmpathVirtual Machine” -SwitchName “External1”

#Configure Dynamic Memory

Set-VMMemory -VMName “VM$i” -DynamicMemoryEnabled $True -MaximumBytes 8GB -MinimumBytes 512MB -StartupBytes 1GB

#Start the VM

Start-VM “VM$i”

#Add the VM to the cluster

Add-ClusterVirtualMachineRole -Cluster “hvc1” -VMName “VM$i”


6 thoughts on “PowerShell Script to Create Lots of Windows Server 2012 Hyper-V Virtual Machines at Once”

  1. With 2012, wouldn’t a differencing disk array in production be appropriate, particularly if you are on a CSV bound to an EMC SAN with something like FAST enabled (moves hot blocks up to SSDs)?

    1. Not for server VMs that are going to be around for years, constantly changing, having variety, etc: service packs, patches, prereqs, software, and so on. It’s fine for short-lived pooled VMs and lab stuff, but not for anything that will exist for a long time and be unique.

  2. All very useful Aidan, thanks again! I am preparing my 2012 server infrastructure exams and read quite a lot from your site. Next time e2evc again??

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.