Managing virtual machines using PowerShell offers IT administrators unparalleled control and automation capabilities. With Hyper-V 2019, Microsoft continues to enhance support for PowerShell, making it an excellent choice for those who prefer scripting to GUI-based management. This beginner’s guide introduces essential PowerShell commands and concepts to help users manage Hyper-V 2019 virtual machines efficiently and confidently.
PowerShell enables automation, consistency, and scalability in managing virtual machine environments. For large infrastructure deployments or repetitive tasks, PowerShell simplifies complex operations with just a few lines of code.
Before using PowerShell for Hyper-V tasks, ensure that the Hyper-V role is installed and that the Hyper-V PowerShell module is available. You can verify this with the following command:
Get-Command -Module Hyper-V
This command lists all available Hyper-V cmdlets on the system. If the module is missing, install it via the Server Manager or use the following PowerShell command:
Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -Restart
To create a new VM, use the New-VM
cmdlet:
New-VM -Name "Win10VM" -MemoryStartupBytes 2GB -Generation 2 -NewVHDPath "C:\VMs\Win10VM.vhdx" -NewVHDSizeBytes 60GB -Path "C:\VMs"
This command creates a Generation 2 VM named Win10VM with 2GB of startup memory and a 60GB virtual hard disk.
Start-VM -Name "Win10VM"
Stop-VM -Name "Win10VM"
These commands power on and shut down the specified virtual machine, respectively.
To get a list of all VMs hosted on a Hyper-V server, use:
Get-VM
This simple command provides the name, state, uptime, and status of each VM.
You can adjust the memory or CPU settings of a VM using cmdlets like:
Set-VM -Name "Win10VM" -MemoryStartupBytes 4GB
Similarly, to change the number of virtual processors:
Set-VMProcessor -VMName "Win10VM" -Count 2
To delete a VM and its associated files:
Remove-VM -Name "Win10VM"
Use caution with this command, as deletions are permanent and cannot be undone.
Get-Help
: Run Get-Help [Cmdlet]
for usage details.PowerShell provides a robust toolkit for managing Hyper-V 2019 virtual machines. Whether creating new VMs, adjusting their settings, or automating maintenance tasks, PowerShell allows for scalable and efficient administration. As proficiency grows, users can explore advanced scripting to further automate and customize their virtual environments.
-ComputerName
parameter with most cmdlets to target a remote Hyper-V host. Export-VM -Name "VMName" -Path "DestinationPath"
to back up a virtual machine.