Powershell & Hyper-V

One thing I’ve only come to appreciate lately is how quick it can be to do things in powershell vs. toiling with virtmgmt ui, let alone fussing with the firewall. Here is a quick script for adding an existing VHDX


# existing VHDX located at D:\myvm\myvm.vhdx
# 4GB RAM, 1 proc, use switch called GuestVirtualSwitch
# secure boot off (Linux, possible to have On with effort https://www.altaro.com/hyper-v/hyper-v-2016-support-linux-secure-boot/ )

$vmname="MYVM"; $procs=1; $vhdpath="T:\MYVM\MYVM.vhdx"; $mem=4GB; $disksize = 128GB; $secureboot="On"
$iso = "b:\software\linux_os_install.iso" 

New-VHD -Path $vhdpath -SizeBytes $disksize
New-VM -Name $vmname -MemoryStartupBytes $mem -Switch GuestVirtualSwitch -BootDevice VHD -Generation 2 -VHDPath $vhdpath
Set-VMProcessor $vmname -Count $procs
Set-VM -VMName $vmname -CheckpointType Disabled
Set-VMFirmware $vmname -EnableSecureBoot $secureboot
Add-VMDvdDrive -VMName $vmname -Path $iso
$dvd = Get-VmDvdDrive -VMName $VMName
Set-VMFirmware -VMName $vmname -FirstBootDevice $dvd
Start-VM $vmname

Leave a Reply

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

OR

LOGIN OR REGISTER

Registered users with one approved comment can comment without moderation