How to Use PowerShell to Manage Hyper-V on Windows Server 2012 R2

PowerShell is the most powerful and efficient way to manage Hyper-V on Windows Server 2012 R2. The Hyper-V module contains over 150 cmdlets covering every aspect of virtual machine lifecycle management, virtual switch configuration, storage management, networking, replication, and host configuration. Mastering PowerShell for Hyper-V enables automation, scripted deployments, bulk operations, and remote management — tasks that would be impractical through the GUI alone.

Prerequisites

  • Windows Server 2012 R2 with the Hyper-V role installed
  • PowerShell 4.0 (included with Windows Server 2012 R2)
  • The Hyper-V PowerShell module (installed automatically with the Hyper-V role)
  • Administrative privileges

Step 1 — Load and Verify the Hyper-V Module

# Import the Hyper-V module (auto-imported in PS 3+, but explicit import ensures it's loaded):
Import-Module Hyper-V

# List all available Hyper-V cmdlets:
Get-Command -Module Hyper-V | Measure-Object

# View cmdlets related to virtual machines:
Get-Command -Module Hyper-V -Noun VM*

Step 2 — Managing Virtual Machines

Create, start, stop, and remove virtual machines:

# Create a new Generation 2 VM:
New-VM -Name "AppServer01" -Generation 2 -MemoryStartupBytes 4GB -SwitchName "ExternalSwitch" -NewVHDPath "D:VMsAppServer01.vhdx" -NewVHDSizeBytes 80GB

# Start a VM:
Start-VM -Name "AppServer01"

# Gracefully shut down a VM (requires Integration Services):
Stop-VM -Name "AppServer01" -Force

# Force power off (equivalent to pulling the plug):
Stop-VM -Name "AppServer01" -TurnOff

# Suspend (save state) a VM:
Suspend-VM -Name "AppServer01"

# Resume a saved VM:
Resume-VM -Name "AppServer01"

# Remove a VM (does not delete VHD files by default):
Remove-VM -Name "AppServer01" -Force

Step 3 — Bulk VM Operations

PowerShell excels at bulk operations. Start all VMs in a stopped state:

Get-VM | Where-Object { $_.State -eq "Off" } | Start-VM

Gracefully shut down all running VMs:

Get-VM | Where-Object { $_.State -eq "Running" } | Stop-VM

Get a status report of all VMs:

Get-VM | Select-Object Name, State, MemoryAssigned, CPUUsage, Uptime, Status | Format-Table -AutoSize

Step 4 — Managing Virtual Switches

# List all virtual switches:
Get-VMSwitch

# Create an external switch:
New-VMSwitch -Name "ExternalSwitch" -NetAdapterName "Ethernet" -AllowManagementOS $true

# Create an internal switch (host-to-VM communication, no external access):
New-VMSwitch -Name "InternalSwitch" -SwitchType Internal

# Create a private switch (VM-to-VM only, no host access):
New-VMSwitch -Name "PrivateSwitch" -SwitchType Private

# Remove a switch:
Remove-VMSwitch -Name "PrivateSwitch" -Force

Step 5 — Managing Virtual Hard Disks

# Create a new fixed VHDX:
New-VHD -Path "D:VMsNewDisk.vhdx" -SizeBytes 100GB -Fixed

# Get details about a VHD:
Get-VHD -Path "D:VMsNewDisk.vhdx"

# Resize a VHDX (expand only — VM must be off or disk must be on SCSI):
Resize-VHD -Path "D:VMsNewDisk.vhdx" -SizeBytes 200GB

# Compact a dynamic VHDX to reclaim unused space (VM must be off):
Optimize-VHD -Path "D:VMsAppServer01.vhdx" -Mode Full

# Mount a VHDX on the host (for file-level access without starting the VM):
Mount-VHD -Path "D:VMsAppServer01.vhdx" -ReadOnly
Dismount-VHD -Path "D:VMsAppServer01.vhdx"

Step 6 — Managing Checkpoints

# Create a checkpoint:
Checkpoint-VM -Name "AppServer01" -SnapshotName "Pre-Update-$(Get-Date -Format 'yyyyMMdd')"

# List all checkpoints:
Get-VMCheckpoint -VMName "AppServer01"

# Restore to a checkpoint:
Restore-VMCheckpoint -Name "Pre-Update-20240515" -VMName "AppServer01"

# Remove a checkpoint:
Remove-VMCheckpoint -VMName "AppServer01" -Name "Pre-Update-20240515"

Step 7 — Remote Hyper-V Management

Manage remote Hyper-V hosts directly from PowerShell:

# Run Hyper-V cmdlets against a remote host:
Get-VM -ComputerName "HV-Host02"

# Create a VM on a remote host:
New-VM -ComputerName "HV-Host02" -Name "RemoteVM01" -Generation 2 -MemoryStartupBytes 2GB -SwitchName "VMSwitch" -NewVHDPath "D:VMsRemoteVM01.vhdx" -NewVHDSizeBytes 40GB

# Use PowerShell remoting for multiple operations:
$Session = New-PSSession -ComputerName "HV-Host02"
Invoke-Command -Session $Session -ScriptBlock {
    Get-VM | Select-Object Name, State, MemoryAssigned
}
Remove-PSSession $Session

Step 8 — Scripted VM Deployment

A practical script to deploy multiple VMs from a template VHDX:

$TemplatePath = "D:TemplatesWS2012R2-Sysprep.vhdx"
$VMList = @("WebServer01","WebServer02","WebServer03")
$VMStoragePath = "D:VMs"
$SwitchName = "ExternalSwitch"

foreach ($VMName in $VMList) {
    $VHDPath = "$VMStoragePath$VMName$VMName.vhdx"
    New-Item -ItemType Directory -Path "$VMStoragePath$VMName" -Force | Out-Null
    New-VHD -Path $VHDPath -ParentPath $TemplatePath -Differencing
    New-VM -Name $VMName -Generation 2 -MemoryStartupBytes 2GB -SwitchName $SwitchName -VHDPath $VHDPath -Path "$VMStoragePath$VMName"
    Set-VMProcessor -VMName $VMName -Count 2
    Set-VMMemory -VMName $VMName -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes 4GB
    Start-VM -Name $VMName
    Write-Host "Deployed: $VMName"
}

Step 9 — Hyper-V Host Configuration

# View host-level settings:
Get-VMHost

# Set default VM and VHD storage paths:
Set-VMHost -VirtualMachinePath "D:VMs" -VirtualHardDiskPath "D:VMsVHDs"

# Enable Enhanced Session Mode (for better RDP-to-console experience):
Set-VMHost -EnableEnhancedSessionMode $true

# Set the number of live migrations allowed simultaneously:
Set-VMHost -MaximumVirtualMachineMigrations 4 -MaximumStorageMigrations 2

Step 10 — Generating VM Inventory Reports

$Report = Get-VM | ForEach-Object {
    $VM = $_
    $Mem = Get-VMMemory -VMName $VM.Name
    $Proc = Get-VMProcessor -VMName $VM.Name
    $Disks = Get-VMHardDiskDrive -VMName $VM.Name
    [PSCustomObject]@{
        Name = $VM.Name
        State = $VM.State
        Generation = $VM.Generation
        vCPUs = $Proc.Count
        MemoryGB = [math]::Round($Mem.Startup / 1GB, 1)
        DiskCount = ($Disks | Measure-Object).Count
    }
}
$Report | Format-Table -AutoSize
$Report | Export-Csv -Path "C:ReportsVMInventory.csv" -NoTypeInformation

Summary

The Hyper-V PowerShell module on Windows Server 2012 R2 provides complete programmatic control over every aspect of your virtualisation infrastructure. From individual VM lifecycle management to bulk operations, scripted deployments, remote host administration, and inventory reporting, PowerShell is the essential tool for any administrator managing more than a handful of VMs. Building a library of reusable Hyper-V PowerShell scripts dramatically reduces the time spent on routine management tasks and ensures consistent, repeatable configuration across your environment.