DEV Community

loading...

Automate Hyper-V VM Creation with Powershell

joeneville_ profile image Joe Neville ใƒป2 min read

If you're using Hyper-V for your virtualization, the Hyper-V Manager GUI provides a serviceable, if a little dull, interface.
However, the bland colour scheme is not the only problem with the graphical interface, its true weakness is just how limited and inefficient it is. Using the wizard, only basic functionality is available, forcing users to click through the presented screens, before going into the virtual machine's 'Settings' to select such rudimentary features as the number of CPUs and Networking configuration.

If you have been paying attention to the messaging coming from Microsoft for years now, it should be no surprise that automating your operations with powershell scripting is a more efficient alternative to all that mouse clicking.

If you dislike powershell, I can sympathize with you, I find it to be quite an ugly customer when compared to the elegance of well-written Python. But, this isn't a beauty contest, we are here to get the job done and leave work on time. Powershell for VM creation it is then!

Sample powershell script for a new VM

Requirements

Specs ๐Ÿ“

  • A new VM with 2 CPUs, 8GB RAM and 20GB storage.

Networking ๐Ÿค

  • The VM should be attached via its 'port1' to my local vswitch, named "vswitch1".
  • The port will be an access port in VLAN199.

To Finish โŒ›๏ธ

  • Once built, start the VM.

Script

# This script is in two parts. First we declare the variables to be applied.

$vm = "my_new_vm" # name of VM, this just applies in Windows, it isn't applied to the OS guest itself.
$image = "path\to\vm\iso\file.iso"
$vmswitch = "vswitch1" # name of your local vswitch
$port = "port1" # port on the VM
$vlan = 199 # VLAN that VM traffic will be send in
$cpu =  2 # Number of CPUs
$ram = 8GB # RAM of VM. Note this is not a string, not in quotation marks
$path_to_disk = "path\to\disk" # Where you want the VM's virtual disk to reside
$disk_size = 20GB # VM storage, again, not a string

# The following are the powershell commands

# Create a new VM
New-VM  $vm
# Set the CPU and start-up RAM
Set-VM $vm -ProcessorCount $cpu -MemoryStartupBytes $ram 
# Create the new VHDX disk - the path and size.
New-VHD -Path $path_to_disk$vm-disk1.vhdx -SizeBytes $disk_size
# Add the new disk to the VM
Add-VMHardDiskDrive -VMName $vm -Path $path_to_disk$vm-disk1.vhdx
# Assign the OS ISO file to the VM
Set-VMDvdDrive -VMName $vm -Path $image
# Remove the default VM NIC named 'Network Adapter'
Remove-VMNetworkAdapter -VMName $vm 
# Add a new NIC to the VM and set its name
Add-VMNetworkAdapter -VMName $vm -Name $port
# Configure the NIC as access and assign VLAN
Set-VMNetworkAdapterVlan -VMName $vm -VMNetworkAdapterName $port -Access -AccessVlanId $vlan
# Connect the NIC to the vswitch
Connect-VMNetworkAdapter -VMName $vm -Name $port -SwitchName $vmswitch
# Fire it up ๐Ÿ”ฅ
Start-VM $vm

Now the VM will boot and you can complete the install process.

Discussion

pic
Editor guide