← script library

Performing System Health Checks on Hyper-V Virtual Machines with PowerShell

Hyper-VDecember 23, 2024

Keeping your Hyper-V virtual machines in optimal health is crucial for ensuring the reliability and performance of your IT infrastructure. This PowerShell script automates the process of checking the health of all Hyper-V virtual machines, providing key metrics such as status, uptime, CPU usage, and memory allocation. This script will: 1. Retrieve the status of each virtual machine. 2. Check CPU and memory utilization. 3. Output detailed health information for each VM. By implementing this script, administrators can quickly identify any issues with their Hyper-V environment and take necessary actions to maintain system health.

powershell
# Import the Hyper-V module
Import-Module Hyper-V
# Get all virtual machines
$vms = Get-VM
# Output health information for each VM
Write-Host "=== Hyper-V Virtual Machine Health Check ==="
foreach ($vm in $vms) {
    $status = $vm.State
    $uptime = (Get-Date) - $vm.Uptime
    $cpuUsage = (Get-VMProcessor -VM $vm).PercentageProcessorTime
    $memoryAssigned = "{0:N2} GB" -f ($vm.MemoryAssigned / 1GB)
    Write-Host "VM Name: $($vm.Name)"
    Write-Host "  Status: $status"
    Write-Host "  Uptime: $([math]::round($uptime.TotalHours, 2)) hours"
    Write-Host "  CPU Usage: $cpuUsage %"
    Write-Host "  Assigned Memory: $memoryAssigned"
    Write-Host "-------------------------------------"
}
Write-Host "Health check completed."

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.