PowerShell’s Docs PowerShell Hyper-V Management How to create a Hyper-V VM with PowerShell

In this new post of my Hyper-V management series, I will explain how create a Hyper-VM with PowerShell.

To create a new virtual machine on a Hyper-V host, follow these steps:

Launch an elevated PowerShell console and run the New-VM cmdlet, as shown below:

New-VM -Name "Kali Linux" -MemoryStartupBytes 4GB -Path "D:\VM\Kali Linux" -NewVHDPath "D:\VM\Kali Linux\Kali Linux.vhdx" -NewVHDSizeBytes 40GB -Generation 2 -SwitchName "Ethernet"
Create new Kali Linux VM in Hyper V using PowerShell

Create new Kali Linux VM in Hyper V using PowerShell

This command creates a new Generation 2 VM named Kali Linux. The parameter names used with this command are pretty much self-explanatory, but let me briefly explain each:

  • -Name: Specifies the VM name.
  • -MemoryStartupBytes:Specifies the amount of static memory in bytes that will be available for VM at startup.
  • -Path:Specifies the directory path to store VM configuration files.
  • -NewVHDPath:Creates and connects a new dynamic VHD with the specified path to the new VM. To connect a VHD that already exists, use the -VHDPath parameter instead of the -NewVHDPath parameter. To create a new VM without any VHD at all, use the -NoVHD
  • -NewVHDSizeBytes:Specifies the size of the VHD in bytes.
  • -Generation:Specifies the generation of the VM. In Windows, you can specify an integer value of "1" (to create a Generation 1 VM) or "2" (to create a Generation 2 VM).

The generations in Hyper-V basically distinguish the VM on the basis of firmware. Generation 1 uses legacy BIOS firmware, whereas Generation 2 uses newer UEFI firmware supporting modern security features, such as secure boot, encryption, and TPM. If you're going to run a modern operating system that utilizes hardware encryption (e.g., Windows 11), you should create a Generation 2 VM. The Generation 2 VM can boot only from a VHDx disk; the VHD disk can be used as a regular data disk.

  • -SwitchName: Specifies the name of the virtual switch to which you want to connect the new VM

The next step is to create a virtual DVD drive and then mount the bootable ISO file so that you can start the OS installation. If you want to perform the OS deployment using a network, you can skip these steps and start the VM directly. To add a virtual DVD drive and mount the ISO file, run the following command:

Add-VMDvdDrive -VMName "Kali Linux" -Path "D:\ISO\kali-linux-2022.1-amd64.iso"
Add virtual DVD drive to VM and Mount the ISO File using PowerShell

Add virtual DVD drive to VM and Mount the ISO File using PowerShell

This command adds a virtual DVD drive to the VM and then mounts the bootable ISO file specified by the -Path parameter. You can use the Get-VMDvdDrive cmdlet to see the details of the virtual DVD drive.

The next step is to check and modify the boot order so that the VM can boot using the mounted ISO file. This setting is available under the firmware of a VM. So, let's find out whether PowerShell gives us any cmdlet to work with VM firmware.

Get-Command -Noun "*firmware*" -Module Hyper-V
Get VM Firmware related Cmdlets

Get VM Firmware related Cmdlets

This command shows the Get-VMFirmware and Set-VMFirmware cmdlets having firmware in their noun sections that are available under the Hyper-V PS module. If we take a look at the help section of the Set-VMFirmware cmdlet, you can see that this cmdlet accepts the -FirstBootDevice parameter.

Set VMFirmware Cmdlet Help

Set VMFirmware Cmdlet Help

So, we can use the Set-VMFirmware cmdlet to change the boot order for the VM, as shown in the following command:

Set-VMFirmware -VMName "Kali Linux" -BootOrder $(Get-VMDvdDrive -VMName "Kali Linux"), $(Get-VMHardDiskDrive -VMName "Kali Linux"), $(Get-VMNetworkAdapter -VMName "Kali Linux")
Set VMFirmware change boot order using PowerShell

Set VMFirmware change boot order using PowerShell

This command changes the boot order for the VM using the -BootOrder parameter. This parameter can accept the values of VMDVDDrive, VMHardDiskDrive, and VMNetworkAdapter. I used the technique of simultaneous variable substitution and command execution to get the VM component object for the -BootOrder parameter on the fly.

After setting the boot order, you can verify it using the Get-VMFirmware cmdlet.

The steps for the entire VM creation, ISO file mounting, and boot order change can also be performed easily using a single PowerShell script, as shown below:

# Define the VM name
$VM = "Kali Linux"

# Define the switch name
$Switch = "vEthernet"

# Define the install media path
$InstallMedia = "D:\ISO\kali-linux-2022.1-amd64.iso"

# Define the VM path
$VMPath = "D:\VM\$VM"

# Define the VHD path
$VHD = "$VMPath\$VM.vhdx"

# Creating a new VM
New-VM -Name $VM -MemoryStartupBytes 4GB -Path $VMPath -NewVHDPath $VHD -NewVHDSizeBytes 40GB -Generation 2 -SwitchName $Switch

# Adding the virtual DVD drive and mounting the ISO file
Add-VMDvdDrive -VMName $VM -Path $InstallMedia

# Configuring the boot order to DVD, VHD, and Network
Set-VMFirmware -VMName $VM -BootOrder $(Get-VMDvdDrive -VMName $VM), $(Get-VMHardDiskDrive -VMName $VM), $(Get-VMNetworkAdapter -VMName $VM)

You can copy this code to the PowerShell ISE and execute it to create and modify the boot order for the new VM. Before executing it, make sure you adjust the variable values as required. After following these steps, your new VM is ready to start up and deploy the OS. In the next section, we will discuss how to manage Hyper-V VMs with PowerShell.

Articles in seriesManage Hyper-V with PowerShell

Discussion (1)

  1. Thank you for writing this article.
    I wrote all my own Powershell code to install VMs, but on the free Hypervisor-only platform. IMO, it is pointless to host VMs on a GUI based server. The command line based server has far less bloat and better security.

    avatar

Leave a Reply

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

Articles in seriesManage Hyper-V with PowerShell
© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account