A friend of mine works for a software company, looking after a test rig for one of their product families. The test rig is based on Hyper-V, and they have to frequently provision lots of virtual machines to test new products. We were chatting about how they do this a few weeks ago and I thought “I bet I could do that using PowerShell, and I bet I could create a very nice solution too”. What I wanted: A PowerShell script that would make lots of Windows Server 2012 (WS2012) Hyper-V VMs from a comma separated values (CSV) file. I also thought that this would be a great demonstration of PowerShell during WS2012 Hyper-V demos that I’m frequently doing.
So I put some work into it. The concept is based on a previous script that I wrote, but I took it to the Nth degree.
Here’s how my script works:
- You create a CSV file that specifies the configuration of each VM (1 row per VM).
- You edit the script to read that file (easy change near the top)
- You run the script
- The results are recorded in a log file that you specify in the script (a second easy change near the top)
I’ve put instructions in the script. I’ve also supplied a sample CSV file in the zip file to show three different types of VHDX creation.
The script allows you to specify the following in the CSV:
- Storage location of the VM
- Name of the VM
- Processor count
- Virtual memory, including Dynamic Memory if you want
- VHDX type and configuration, including Fixed, Dynamic, and Differencing
So how could you use the script?
- Maybe you want to create lots of pooled or personal VMs
- Possibly you need to deploy a demo lab with lots of VMs
- Maybe you are like my friend, working in a large software test environment, and you’d like to deploy lots of VMs, and you’d like to be able to vary the specification and build of those VMs
There’s nothing to stop you from editing the script and either adding/removing functionality. If you find it useful, then go ahead and use it. In fact, if you do use it, then tell me how because I’ll be interested.
This script is not supported by me. Use and/or modify the script at your own risk.
Please note that this script is mine and you may not re-share it without my permission. Please link to this blog post instead.
You can download the script and sample CSV file from here.
Possible improvements:
- Add remote host functionality
- Add cluster functionality
- Start VMs
This information has been brought to you by Windows Server 2012 Hyper-V Installation and Configuration Guide (available on pre-order on Amazon) where you’ll find lots of PowerShell like in this script:
I’ve done this with XML file and powershell for my demo-lab. the xml includes the needed vm config with network configuration and the script mounts an unattended iso for server 2012 to the vms. i can set up my needed lab with an basic server installation for all vms fully automatic. very time-saving and helpful 🙂 like this blogpost – good job
Is there a way to create VMs based off a single image where only the differences between each VM and the image are stored? In the VMware world, I think Orchestrator does this for VMs by creating a “golden image” and then spawning VMs based on that image. The advantages include less storage required and the ability to patch the golden image and all VMs get patched.
That’s a differencing disk. However it’s only intended for things like testing or pooled VDI where the VMs are short-lived. You can’t patch a differencing disk’s parent VHD/X … you have to tear down the VMs, replace the parent with a patched version, and start over.
Hi Aidan,
A very big thanks for this script, you just made my day (night actually), once more!
i’m teaching a WS2012/W8 crash course, and this script will be featured tomorrow to my students (directly from your blog that is!)…
best regards, and a big thanks once again.
Pierre.
Thanks Pierre! You just made my night too 🙂
Optimizations 🙂
PS C:Temp> if(1MB -eq 1048576) {Write-Host “So 1MB is in fact” (1MB)}
So 1MB is in fact 1048576
PS C:Temp> if(1GB -eq 1073741824) {Write-Host “And 1GB is” (1GB)}
And 1GB is 1073741824
PS C:Temp>
hey mate, your link to the VMsFromCSV.zip file is broken?
Hi Aidan,
The link for the script and sample CSV file is not working. Can you please send us another link? 🙂
Thanks in advance!
for those looking for the CSV (sorry so late) but it looks like the Wayback machine came in to the rescue here… Check this link for an old version of the file from March and June 2013. Hopefully it’ll work for you too!
https://web.archive.org/web/*/https://aidanfinn2.wpcomstaging.com/Downloads/VMsFromCSV.zip