The article is devoted to the features of managing Hyper-V virtual machines from the PowerShell console. We will look at creating virtual switches and virtual machines, changing VM settings and managing them. You can use the above commands to manually manage your VMs or in PowerShell scripts to automate various tasks.
Installing the Hyper-V role in Windows Server and Windows 10.
To install the Hyper-V role, the host must have a processor that supports virtualization with SLAT. On Windows Server, the following command is used to install the Hyper-V role:
Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -RestartIn desktop editions (Windows 10 and 11), the Hyper-V role is installed as follows:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V –AllTo manage a Hyper-V host, the computer must have the Hyper-V plug-in installed. The full list of commands in the module (depending on the version of Windows) can be displayed like this:
Get-Command -Module hyper-v
In Windows Server 2022, there are 245 cmdlets available in the Hyper-V module.
You can display a complete list of Hyper-V host settings using the command:
Get-VMHost|fl *To display only information about the number of available cores and RAM:
Get-VMHost| select LogicalProcessorCount, MemoryCapacity
To change Hyper-V host settings, use the Set-VMHost cmdlet. The following command will change the default paths for storing virtual disks and VM configuration files:
Set-VMHost -VirtualMachinePath D:\VM -VirtualHardDiskPath 'D:\VM\VHD'
Create a Hyper-V Virtual Switch Using PowerShell.
The first step is to create a virtual switch on the Hyper-V server. Virtual machines will only be able to access the network through the virtual switch.
Let's list the available physical adapters on the Hyper-V host:
Get-NetAdapter | where {$_.status -eq "up"}
If your server supports SR-IOV (Single-Root Input/Output (I/O) Virtualization) , please note that you need to enable this option when creating the switch. You cannot enable SR-IOV for an existing vSwitch.
Create a virtual external switch:
New-VMSwitch -Name "ExtVMSwitch" -AllowManagementOS $True -NetAdapterName Ethernet0 -SwitchType External
Create and Modify Hyper-V Virtual Machine Settings Using PowerShell.
To create a new virtual machine, use the New-VM cmdlet. In this example, we will create a new generation 2 VM with 1 GB of RAM and a 5 GB vhdx disk.
$VMName = "spb-dmz2"
$VM = @{
Name = $VMName
MemoryStartupBytes = 1Gb
Generation = 2
NewVHDPath = "C:\HV\$VMName\$VMName.vhdx"
NewVHDSizeBytes = 5Gb
BootDevice = "VHD"
Path = "C:\HV\$VMName"
SwitchName = "ExtVMSwitch"
}
New-VM @VM
Let's look at the commands that can be used to change the settings of virtual machines.
Increase RAM size for VM:
Get-VM -Name spb-dmz1| Set-VMMemory -StartupBytes 2GbChange number of vCPUs:
Set-VMProcessor spb-dmz1 -Count 2Allow autostart for Hyper-V virtual machine:
Get-VM –VMname spb-dmz1 | Set-VM –AutomaticStartAction StartTo mount an additional virtual disk in a VM, you must first create it:
New-VHD -Path 'C:\VM\test1.vhdx' -SizeBytes 2GBAnd then connect to the VM:
Add-VMHardDiskDrive -VMName spb-dmz1 -Path 'C:\VM\test1.vhdx'
Using PowerShell to Manage Hyper-V Virtual Machines.
List virtual machines on a Hyper-V host:Get-VMThe command returned a list of VMs with a few basic characteristics. To display all VM properties, run:
Get-VM -Name spb-dmz1 | fl *Display only enabled VMs:
Get-VM | where {$_.State -eq 'Running'}Start virtual machine:
Start-VM -Name spb-app01Start all shutdown virtual machines:
Get-VM | where {$_.State -eq 'Off'} | Start-VMShutdown VM (correct shutdown via guest OS):
Stop-VM -Name spb-app01To power off the VM, use the TurnOff key:
Stop-VM -Name spb-app01 –TurnOffMount ISO file to virtual CD/DVD device:
Set-VMDvdDrive -VMName spb-app01 -Path c:\iso\WinSrv2022.isoTo transfer all VM files on the fly to another disk, use the command:
Move-VMStorage spb-app01 -DestinationStoragePath D:\VM\spb-app01You can enlarge or shrink a virtual disk using the Resize-VHD command:
Resize-VHD -Path 'C:\VM\fs01.vhdx' -SizeBytes 50GbCreate a checkpoint (snapshot) of the specified VM:
Get-VM -Name spb-app01| Checkpoint-VM -SnapshotName "before install patch"
Display a list of available checkpoints:
Return the state of the VM from the previous checkpoint:Restore-VMCheckpoint -Name "before install patch" -VMName spb-app01 -Confirm:$falseDelete snapshot:
Remove-VMCheckpoint -VMName spb-app01 -Name "before install patch"Export, import and cloning of VMs are described in detail in the article at the link:
Export-VM -Name spb-app01 -Path 'C:\VHD\export' -CaptureLiveState CaptureCrashConsistentState
You can use the built-in Windows Server Backup to back up Hyper-V virtual machines.Get IP addresses of guest OS virtual machines:
Get-VM | Select -ExpandProperty NetworkAdapters | Select VMName, IPAddresses, StatusConnect to the console of a specific virtual machine:
vmconnect.exe localhost spb-app01
To connect a PowerShell session directly to the guest OS of virtual machines via the vmbus, you can use PowerShell Direct (available for Windows Server 2016, Windows 10 and newer guest OS). You can use the Invoke-Command (to run scripts) and Enter-PSSession (to enter an interactive PowerShell session) cmdlets:
Invoke-Command -VMName spb-app01 -ScriptBlock {Get-Process}To copy files from a Hyper-V host to a virtual machine using PowerShell Direct, use:
Enter-PSSession -VMName spb-app01
$PSSession1 = New-PSSession --VMName spb-app01 -Credential (Get-Credential)You can use PowerShell to manage virtual machines locally or remotely on Hyper-V hosts (both on Windows Server in Full GUI or Core modes, or on Free Windows Hyper-V Server, or Windows 10) either alone or in addition to graphical management tools Hyper-V Manager and Windows Admin Center.
Copy-Item -ToSession $PSSession1 -Path C:\iso\win10.iso -Destination D:\ISO\