Reading Time: 1 minute
Share:
Twitter
LinkedIn
Facebook
Reddit
Whatsapp
Follow by Email

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
view raw Hyper-V_New_VM.ps1 hosted with ❤ by GitHub

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.

Share:
Twitter
LinkedIn
Facebook
Reddit
Whatsapp
Follow by Email

Pixel Robots.

I’m Richard Hooper aka Pixel Robots. I started this blog in 2016 for a couple reasons. The first reason was basically just a place for me to store my step by step guides, troubleshooting guides and just plain ideas about being a sysadmin. The second reason was to share what I have learned and found out with other people like me. Hopefully, you can find something useful on the site.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

I agree