Introduction
Virtual machines are essential tools for creating isolated environments. VMs are good for testing, development, or running multiple operating systems on a single host. PowerShell simplifies the process of managing these VMs, allowing you to automate tasks like creating virtual hard drives, configuring boot orders, and more.
In this guide, we’ll explore step-by-step examples to help you efficiently set up and manage VMs using PowerShell.
Creating a Virtual Machine with a New VHD Drive
By using PowerShell, you can automate the process of creating a virtual machine with a new virtual hard drive (VHD). This section will guide you through the necessary steps.
Command Breakdown
New-VM
And here are the necessary parameters:
- -Name: Specifies the name of the virtual machine.
- -MemoryStartupBytes: Allocates the amount of memory the VM will use at startup.
- -Path: Defines the directory where the VM’s files will be stored.
- -NewVHDPath: Specifies the location and name for the new virtual hard drive.
- -NewVHDSizeBytes: Sets the size of the new VHD in bytes.
- -Generation: Chooses between Generation 1 and Generation 2 VMs, with Generation 2 offering more modern features.
- -SwitchName: Connects the VM to a virtual switch for network access
Example create a new VM
New-VM -Name "VM-Win11" ` -MemoryStartupBytes 4GB ` -Path "C:\Virtual Machine Files\Virtual Machines\VM-Win11" ` -NewVHDPath "C:\Virtual Hard Disk Files\VM-Win11.vhdx" ` -NewVHDSizeBytes 10GB ` -Generation 2 ` -SwitchName "V-Switch-Internal"
This script creates a VM called “VM-Win11” with 4GB of startup memory, a 10GB VHD, and connects it to the “V-Switch-Internal.” The VM files and VHD are stored in the specified directories.
To verify the status of the VM, use Get-VM cmdlet:
Get-VM -VMName "VM-Win11"
To list the virtual hard disks attached to the VM-Win11:
Get-VMHardDiskDrive -VMName "VM-Win11"
ControllerLocation: Indicates the specific slot on the controller where a device (like a virtual hard disk or DVD drive) is connected.
- 0: Represents the first location or slot on the controller.
- 1: Represents the second location or slot on the controller.
In Generation 2 VMs, most drives are connected via SCSI controllers, which are more flexible. SCSI controllers can have many locations (not limited to just 0 and 1).
Adding a Virtual DVD Drive and Mounting an ISO File
After creating the VM, you’ll need to install an operating system. This is done by adding a virtual DVD drive and mounting an ISO file:
Add-VMDvdDrive
This command comes with two parameters
- -VMName: Specifies the name of the VM to which the DVD drive will be added.
- -Path: Defines the path to the ISO file to be mounted.
Here is an example:
Add-VMDvdDrive -VMName "VM-Win11" -Path "D:\Win11_23H2_English_x64v2.iso"
This command adds a virtual DVD drive to the “VM-Win11” VM and mounts the Windows 11 ISO file, allowing you to install the OS on the newly created VHD.
To get the DVD drives attached to VM-Win11, use the Get-VMDvdDrive cmdlet:
Get-VMDvdDrive -VMname "VM-Win11"
Configuring Boot Order
It is essential to configure the boot order for the new created VM. The following command sets the VM to boot first from the DVD drive, then from the VHD, and finally from the network adapter:
Set-VMFirmware -VMName "VM-Win11" ` -BootOrder $(Get-VMDvdDrive -VMName "VM-Win11"), $(Get-VMHardDiskDrive -VMName "VM-Win11"), $(Get-VMNetworkAdapter -VMName "VM-Win11")
To verify the current boot order use the Get-VMFirmware cmdlet:
Get-VMFirmware -VMName "VM-Win11" | Select-Object -ExpandProperty BootOrder
Putting it all together
Below is an example using what we have learned so far in this section:
# Specify paths for Virtual Hard Disk and Virtual Machines $vhdPath = "D:\Hyper-V\VirtualHardDisk" $vmPath = "D:\Hyper-V\VirtualMachine" # Specify install media path $InstallMedia = "D:\Lab Study\Win11_23H2_English_x64v2.iso" # Specify VM information $VmName = "VM-Win11" $VmMemory = 4GB # Create new VHDX New-VHD -Path "$vhdPath\VM-Win11.vhdx" ` -SizeBytes 10GB ` -Fixed # Specify Virtual Switch $SwitchName = "V-Switch-Internal" # Create new VM New-VM -Name $VmName ` -MemoryStartupBytes $VmMemory ` -Path $vmPath ` -Generation 2 ` -SwitchName $SwitchName # Set number of virtual processors to 2 Set-VMProcessor -VMName $VmName -Count 2 # Attach VHDX to the VM Add-VMHardDiskDrive -VMName $VmName ` -Path "$vhdPath\VM-Win11.vhdx" # Attach ISO to the VM and set it as the first boot order Add-VMDvdDrive -VMName "$VmName" ` -Path $InstallMedia # Set boot order Set-VMFirmware -VMName $VmName ` -BootOrder $(Get-VMDvdDrive -VMName $VmName), $(Get-VMHardDiskDrive -VMName $VmName), $(Get-VMNetworkAdapter -VMName $VmName)
Creating a Virtual Machine Without a VHD Drive
In some scenarios, you might need to create a virtual machine (VM) without attaching a virtual hard disk (VHD) during the initial setup. This approach can be useful if you plan to attach existing VHDs later, boot from a network, or use the VM for testing purposes.
This section will show you how to create a VM without a VHD using PowerShell.
Here’s a practical example of creating a virtual machine named “VM-Win11” without a VHD:
New-VM -Name "VM-Win11" ` -MemoryStartupBytes 4GB ` -Path "C:\Virtual Machine Files" ` -Generation 2 ` -SwitchName "Default Switch" ` -NoVHD
-NoVHD: The VM is created without a virtual hard drive.
Use Cases:
- Network Booting: If you plan to boot the VM over a network using PXE, you might opt to create it without a VHD initially.
- Temporary or Testing VMs: For short-lived or testing VMs, where a VHD isn’t necessary, this approach can save time and resources.
Deleting a Virtual Machine and its VHD Files
In some cases, you may need to completely remove a virtual machine (VM) and its associated virtual hard disks (VHDs) from Hyper-V. This section covers how to use PowerShell to delete both the VM and its VHDs, ensuring that all associated resources are cleaned up.
For instance, to delete a virtual machine named “Test-VM” and its associated VHD file:
$VMName = "Test-VM" $vhdPath = "D:\Hyper-V\VirtualHardDisk\$VMName" $vmPath = "D:\Hyper-V\VirtualMachine\$VMName.vhdx" # Stop the VM if it's running Stop-VM -Name $VMName -Force # Remove the VM and delete its configuration and VHD files Remove-VM -Name $VMName -Force Remove-Item -Path $VMPath -Recurse -Force Remove-Item -Path $VHDPath -Force
Explanation:
- Stop-VM: Stops the VM forcefully if its running.
- Remove-VM: Deletes the VM from Hyper-V.
- Remove-Item: Cleans up the VM’s configuration files and the VHD from the disk.
To further enhance your understanding of managing virtual machines in Hyper-V using PowerShell, here is the link to Microsoft Docs.
Pass-through Disks and Differencing Drives in Hyper-V
These storage options in Hyper-V provide additional flexibility depending on your performance needs and the complexity of your virtual environment.
Pass-through Disks
A Pass-through Disk allows a virtual machine (VM) to directly access a physical hard disk on the Hyper-V host. Instead of using a virtual hard disk (VHD), the VM communicates directly with the host’s physical storage. Pass-through disks are typically used when:
- High performance is required, bypassing the virtualization layer.
- The need to access large amounts of storage directly from the physical disk.
Differencing Drives
A Differencing Drive is a type of virtual hard disk (VHD) that is linked to a parent disk and stores only the changes made since the parent disk was created. This is useful for:
- Snapshots: Managing multiple versions of a base image without duplicating the entire VHD.
- Lab Environments: Quickly creating multiple VMs from a single base VHD for testing.
Conclusion
This blog has demonstrated how to use PowerShell for creating and managing virtual machines in Hyper-V, including configuring VHDs, setting boot orders, and deleting VMs with their associated resources. These techniques provide flexibility and control, enabling efficient and automated VM management. Whether creating new VMs or cleaning up unused ones, you can streamline your virtualization tasks with ease.