Create Hyper-V VMs the easy way

I’ve been doing a lot of Windows Autopilot testing lately and honestly, the biggest pain in doing so is creating VMs to test with. So, I created a PowerShell script that does all that work for me in creating as many VMs as I feel the need for. I thought it was handy enough to share so I put it on the PowerShell Gallery for others to download and use (at your own risk, no warranties, etc. etc. etc.).

While this script was created to help automate the creation of Hyper-V VMs for Windows Autopilot testing, but it can be used to create Hyper-V VMs for any purpose you need.

Run the script with administrative privileges on your Hyper-V host machine.

You can get the script by running this command from an elevated PowerShell console window: Install-Script Create-TestVMs. The script, as all scripts installed from the PowerShell Gallery are, will be saved to: $env:ProgramFiles\WindowsPowerShell\Scripts. Anytime you want to run the script, kick off the fun by running Create-TestVMs in an elevated PowerShell console window.

Here’s what it does

When the script is run, it will prompt you for the various VM settings necessary to create a Hyper-V VM. It will then create, and start, as many VMs as you ask for using the provided naming prefix to begin the VM name. If you do not change the default values at the top of the script, it will create and start a VM named Autopilot-*999*, where *999* is a three digit random number, with 2GB of RAM and the VM config files and hard disks stored in the Hyper-V default locations.

create-testVMs

Default values

If you do not modify the defaults when prompted when the script runs, the following default values will be used:

If you do not modify the defaults when prompted when the script runs, the following default values will be used:

  • $VMPath = C:\ProgramData\Microsoft\Windows\Hyper-V.  This is the Hyper-V  default location for saving VM configuration files. You can browse to the location to save VM config files when the script runs.
  • $HDPath = C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks. Where the VM hard disks will be stored. Again, this is the Hyper-V server default location. When the script runs, you can browse to the location to save VM hard disks.
  • $RAM = 2. Amount of RAM in Gigabytes to assign to new VMs. When the script runs, it will tell you how much RAM is available on your host to help you decide (2GB = 2*1073741824).
  • $VMSwitch = Default Switch. Name of the Hyper-V virtual switch to use. When the script runs, it will tell you what virtual switches are available on your Hyper-V host to help you decide.
  • $ISO = None assigned by default. ISO file to mount on new VMs. You can browse to the location of the ISO when the script runs.
  • $VMPrefix = Autopilot. Name prefix for new VMs. Followed by -<3 random numbers> when the VM is created.
  • $VMnumber = 1. Number of VMs to create.

Modify the defaults

You can accept or modify the default values when prompted as the script runs.

To modify the default values used by the script, edit the default parameter settings at the top of the script. If you installed the script using the Install-Script Create-TestVMs PowerShell command, the script can be found at $env:ProgramFiles\WindowsPowerShell\Scripts.

Feedback or suggestions for improvements? Find me on Twitter @jeffgilb.

Loading