Recently I have managed to get a new work laptop, one that can utilize nested Virtualisation with Hyper-V. Because of this, I needed to create my lab again. I had some spare time so I decided to create a basic script to help me create new Hyper-V Virtual Machines via PowerShell.
The below PowerShell code will create a new Virtual Machine, With 1024MB RAM, GEN 2 VM, and 40GB HDD. It will also attach an ISO so when you start the Virtual Machine you will be ready to install an operating system.
[themify_box style=”note”]
Change the script to match your needs.
[/themify_box]
$VMName = "PIXEL-SRV01" | |
[int64]$VMMem = 1MB*1024 #chnage to 1gb* the number of gb if you need. | |
$VMGen = "2" | |
$VHDPath = "V:\Hyper-V\$VMname\Virtual Hard Disks\$VMName.VHDX" | |
$VMPath = "V:\Hyper-V\" | |
[int64]$VMSize = 1GB*40 | |
$ISOPath = "C:\ISO\en_windows_server_2016_x64_dvd_9327751.iso" | |
new-vm -Name $VMName -path $VMPath -MemoryStartupBytes $VMMem -Generation $VMGen -NewVHDPath $VHDPath -NewVHDSizeBytes $VMSize -BootDevice vhd | |
Add-VMDvdDrive $VMName | |
Start-Sleep -Seconds 5 | |
Set-VMDvdDrive -VMName $VMName -Path $ISOPath -ToControllerNumber 0 -ToControllerLocation 1 | |
Start-Sleep -Seconds 5 | |
$dvd = Get-VMDvdDrive -VMName $VMName | |
Set-VMFirmware -VMName $VMName -FirstBootDevice $dvd | |
Start-Sleep -Seconds 5 |
You can open Hyper-V Manager and you should see your newly Created Virtual Machine, ready for you to power it on and install the Operating System we attached via the ISO.
You can always customize the script above so that it will prompt you for input. I may tidy up this script and add this option in one day. When I do I will upload it to the site.
[AdSense-A]
0 Comments