How to create generation 2 virtual machine using PowerShell?

Below script would help to create virtual machine in Hyper-V using PowerShell-

  1. Open PowerShell window as admin.
  2. Paste below script and hit enter.
  3. VM would be created.
  4. Open Hyper-V Manager and verify it.

# Set VM Name, Switch Name, and Installation Media Path.
$VMName = ‘TestVM’
# Switch is nothing but network adapter
$Switch = ‘My New Virtual Switch’
#OS Iso file should be assigned to InstallMedia
$InstallMedia = ‘D:\Softwares\en_windows_10_enterprise_version_1607_updated_jul_2016_x64_dvd_9054264.iso’

# Create New Virtual Machine
# MemoryStartupBytes contents the RAM size of VM.
# NewVHDSizeBytes contents the size of HD.
New-VM -Name $VMName -MemoryStartupBytes 1073741824 -Generation 2 -NewVHDPath “C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\$VMName.vhdx” -NewVHDSizeBytes 53687091200 -Path “C:\ProgramData\Microsoft\Windows\Hyper-V\$VMName” -SwitchName $Switch

# Add DVD Drive to Virtual Machine
Add-VMScsiController -VMName $VMName
Add-VMDvdDrive -VMName $VMName -Path $InstallMedia

# Mount Installation Media
$DVDDrive = Get-VMDvdDrive -VMName $VMName

# Configure Virtual Machine to Boot from DVD
Set-VMFirmware -VMName $VMName -FirstBootDevice $DVDDrive

Advertisements

One thought on “How to create generation 2 virtual machine using PowerShell?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s