Hyper-V PowerShell Commands
Hyper-V management is primarily done through PowerShell cmdlets. This reference covers the most commonly used commands for VM management, networking, storage, and administration. All commands require the Hyper-V PowerShell module.
Virtual Machine Management
Create a New VM
# Basic VM creation
New-VM -Name "WebServer01" -MemoryStartupBytes 4GB -Generation 2
# Complete VM with virtual switch
New-VM -Name "WebServer01" `
-MemoryStartupBytes 4GB `
-Generation 2 `
-NewVHDPath "C:\VMs\WebServer01.vhdx" `
-NewVHDSizeBytes 80GB `
-Switch "External Switch"
List All VMs
# Get all VMs
Get-VM
# Get VMs with specific state
Get-VM | Where-Object {$_.State -eq 'Running'}
# Get detailed VM information
Get-VM -Name "WebServer01" | Format-List *
Start, Stop, and Restart VMs
# Start a VM
Start-VM -Name "WebServer01"
# Stop a VM (graceful shutdown)
Stop-VM -Name "WebServer01"
# Force stop a VM
Stop-VM -Name "WebServer01" -Force
# Save VM state
Save-VM -Name "WebServer01"
# Restart a VM
Restart-VM -Name "WebServer01"
Configure VM Settings
# Set VM memory
Set-VM -Name "WebServer01" -MemoryStartupBytes 8GB
# Enable Dynamic Memory
Set-VM -Name "WebServer01" -DynamicMemory `
-MemoryMinimumBytes 2GB `
-MemoryMaximumBytes 16GB `
-MemoryStartupBytes 4GB
# Set CPU count
Set-VM -Name "WebServer01" -ProcessorCount 4
# Enable nested virtualization
Set-VMProcessor -VMName "WebServer01" -ExposeVirtualizationExtensions $true
# Configure automatic actions
Set-VM -Name "WebServer01" `
-AutomaticStartAction Start `
-AutomaticStopAction ShutDown
Remove a VM
# Remove VM (keeps VHD)
Remove-VM -Name "WebServer01"
# Remove VM and all VHDs
Remove-VM -Name "WebServer01" -Force
Remove-Item "C:\VMs\WebServer01.vhdx"
Virtual Hard Disk Management
Create Virtual Disks
# Create a fixed size VHDX
New-VHD -Path "C:\VMs\Data.vhdx" `
-SizeBytes 100GB -Fixed
# Create a dynamic VHDX
New-VHD -Path "C:\VMs\Data.vhdx" `
-SizeBytes 100GB -Dynamic
# Create a differencing disk
New-VHD -Path "C:\VMs\Child.vhdx" `
-ParentPath "C:\VMs\Parent.vhdx" `
-Differencing
Manage Virtual Disks
# Attach VHD to a VM
Add-VMHardDiskDrive -VMName "WebServer01" `
-Path "C:\VMs\Data.vhdx"
# Resize a VHDX
Resize-VHD -Path "C:\VMs\Data.vhdx" -SizeBytes 200GB
# Convert VHD to VHDX
Convert-VHD -Path "C:\VMs\Old.vhd" `
-DestinationPath "C:\VMs\New.vhdx"
# Optimize (compact) a VHDX
Optimize-VHD -Path "C:\VMs\Data.vhdx" -Mode Full
# Get VHD information
Get-VHD -Path "C:\VMs\Data.vhdx"
Disk Controller Management
# List all disk controllers
Get-VMScsiController -VMName "WebServer01"
# Add SCSI controller
Add-VMScsiController -VMName "WebServer01"
# Remove a hard drive
Remove-VMHardDiskDrive -VMName "WebServer01" `
-ControllerType SCSI `
-ControllerNumber 0 `
-ControllerLocation 1
Virtual Networking
Create Virtual Switches
# Create external switch
New-VMSwitch -Name "External Switch" `
-NetAdapterName "Ethernet" `
-AllowManagementOS $true
# Create internal switch
New-VMSwitch -Name "Internal Switch" `
-SwitchType Internal
# Create private switch
New-VMSwitch -Name "Private Switch" `
-SwitchType Private
Manage Network Adapters
# Add network adapter to VM
Add-VMNetworkAdapter -VMName "WebServer01" `
-SwitchName "External Switch"
# Configure static MAC address
Set-VMNetworkAdapter -VMName "WebServer01" `
-StaticMacAddress "00-15-5D-00-00-01"
# Enable MAC address spoofing
Set-VMNetworkAdapter -VMName "WebServer01" `
-MacAddressSpoofing On
# Configure VLAN
Set-VMNetworkAdapterVlan -VMName "WebServer01" `
-Access -VlanId 100
# Get network adapter information
Get-VMNetworkAdapter -VMName "WebServer01"
Advanced Networking
# Enable SR-IOV
Set-VMNetworkAdapter -VMName "WebServer01" `
-IovWeight 100
# Configure bandwidth limits
Set-VMNetworkAdapter -VMName "WebServer01" `
-MaximumBandwidth 1000000000
# Enable DHCP Guard
Set-VMNetworkAdapter -VMName "WebServer01" `
-DhcpGuard On
# Enable Router Guard
Set-VMNetworkAdapter -VMName "WebServer01" `
-RouterGuard On
Snapshots (Checkpoints)
Create and Manage Checkpoints
# Create a checkpoint
Checkpoint-VM -Name "WebServer01" `
-SnapshotName "Before Update"
# List all checkpoints
Get-VMSnapshot -VMName "WebServer01"
# Restore to a checkpoint
Restore-VMSnapshot -VMName "WebServer01" `
-Name "Before Update" -Confirm:$false
# Remove a checkpoint
Remove-VMSnapshot -VMName "WebServer01" `
-Name "Before Update"
# Remove all checkpoints
Remove-VMSnapshot -VMName "WebServer01" -IncludeAllChildSnapshots
Configure Checkpoint Type
# Set to Production checkpoints (recommended)
Set-VM -Name "WebServer01" -CheckpointType Production
# Set to Standard checkpoints
Set-VM -Name "WebServer01" -CheckpointType Standard
# Disable automatic checkpoints
Set-VM -Name "WebServer01" -CheckpointType Disabled
Live Migration
Configure and Perform Live Migration
# Enable Live Migration on host
Enable-VMMigration
# Configure Live Migration settings
Set-VMHost -VirtualMachineMigrationAuthenticationType Kerberos `
-VirtualMachineMigrationPerformanceOption SMB `
-MaximumVirtualMachineMigrations 2
# Perform live migration
Move-VM -Name "WebServer01" `
-DestinationHost "HyperV-Host02"
# Live migration with storage migration
Move-VM -Name "WebServer01" `
-DestinationHost "HyperV-Host02" `
-DestinationStoragePath "D:\VMs\WebServer01"
# Storage migration only (same host)
Move-VMStorage -VMName "WebServer01" `
-DestinationStoragePath "D:\VMs\WebServer01"
Replication (Hyper-V Replica)
Configure Replication
# Enable replication on primary host
Set-VMReplication -VMName "WebServer01" `
-ReplicaServerName "HyperV-Replica" `
-ReplicaServerPort 80 `
-AuthenticationType Kerberos `
-CompressionEnabled $true `
-ReplicationFrequencySec 300
# Start initial replication
Start-VMInitialReplication -VMName "WebServer01"
# Get replication status
Get-VMReplication -VMName "WebServer01"
# Perform test failover
Start-VMFailover -VMName "WebServer01" -Prepare -AsTest
# Perform planned failover
Start-VMFailover -VMName "WebServer01" -Prepare
Start-VMFailover -VMName "WebServer01-Replica" -AsJob
# Reverse replication direction
Set-VMReplication -Reverse -VMName "WebServer01"
Integration Services
Manage Integration Services
# Get Integration Services status
Get-VMIntegrationService -VMName "WebServer01"
# Enable specific integration service
Enable-VMIntegrationService -VMName "WebServer01" `
-Name "Guest Service Interface"
# Disable integration service
Disable-VMIntegrationService -VMName "WebServer01" `
-Name "Time Synchronization"
# Enable all integration services
Get-VMIntegrationService -VMName "WebServer01" |
Enable-VMIntegrationService
Import and Export
Export and Import VMs
# Export a VM
Export-VM -Name "WebServer01" `
-Path "C:\Exports"
# Import a VM (register in place)
Import-VM -Path "C:\Exports\WebServer01\config.xml"
# Import and copy VM files
Import-VM -Path "C:\Exports\WebServer01\config.xml" `
-Copy -VhdDestinationPath "D:\VMs" `
-GenerateNewId
# Import as new VM
Import-VM -Path "C:\Exports\WebServer01\config.xml" `
-Copy -GenerateNewId
Host Management
Configure Hyper-V Host Settings
# Get host information
Get-VMHost
# Set default VM paths
Set-VMHost -VirtualHardDiskPath "D:\VMs" `
-VirtualMachinePath "D:\VMs"
# Configure NUMA spanning
Set-VMHost -NumaSpanningEnabled $false
# Get host compute resources
Get-VMHostNumaNode
Get-VMHostSupportedVersion
# Enable Enhanced Session Mode
Set-VMHost -EnableEnhancedSessionMode $true
Resource Metering
# Enable resource metering
Enable-VMResourceMetering -VMName "WebServer01"
# Get resource metrics
Measure-VM -VMName "WebServer01"
# Reset metrics
Reset-VMResourceMetering -VMName "WebServer01"
# Disable metering
Disable-VMResourceMetering -VMName "WebServer01"
Security Configuration
Configure Shielded VMs
# Enable TPM
Set-VMKeyProtector -VMName "WebServer01" `
-NewLocalKeyProtector
Enable-VMTPM -VMName "WebServer01"
# Enable Secure Boot
Set-VMFirmware -VMName "WebServer01" `
-EnableSecureBoot On `
-SecureBootTemplate "MicrosoftWindows"
# Get security settings
Get-VMSecurity -VMName "WebServer01"
Performance Tuning
Optimize VM Performance
# Configure processor settings
Set-VMProcessor -VMName "WebServer01" `
-Count 4 `
-Reserve 10 `
-Maximum 75 `
-RelativeWeight 200
# Configure NUMA topology
Set-VMProcessor -VMName "WebServer01" `
-MaximumCountPerNumaNode 2
# Enable Virtual NUMA
Set-VMProcessor -VMName "WebServer01" `
-ExposeVirtualizationExtensions $true
# Configure memory QoS
Set-VMMemory -VMName "WebServer01" `
-Buffer 20 `
-Priority 80
Useful PowerShell One-Liners
Quick Reference Commands
# Start all stopped VMs
Get-VM | Where-Object {$_.State -eq 'Off'} | Start-VM
# Get VM uptime
Get-VM | Select-Object Name, State, Uptime
# Get VMs using most memory
Get-VM | Select-Object Name, @{N="Memory(GB)";E={$_.MemoryAssigned/1GB}} |
Sort-Object "Memory(GB)" -Descending
# Export all VM names to file
Get-VM | Select-Object Name | Export-Csv "C:\VMList.csv"
# Check which VMs need Integration Services update
Get-VM | Where-Object {$_.IntegrationServicesState -ne 'UpToDate'}
# Get all VMs with Dynamic Memory enabled
Get-VM | Where-Object {$_.DynamicMemoryEnabled -eq $true}
# List all VHD files in use
Get-VM | Get-VMHardDiskDrive | Select-Object VMName, Path
# Find VMs without checkpoints
Get-VM | Where-Object {(Get-VMSnapshot -VMName $_.Name).Count -eq 0}
Installation Commands
Install Hyper-V Role
# Install Hyper-V on Windows Server
Install-WindowsFeature -Name Hyper-V `
-IncludeManagementTools -Restart
# Install Hyper-V PowerShell module only
Install-WindowsFeature -Name Hyper-V-PowerShell
# Enable Hyper-V on Windows 10/11 Pro
Enable-WindowsOptionalFeature -Online `
-FeatureName Microsoft-Hyper-V -All