← script library

Monitor System Health for Hyper-V Virtual Machines with PowerShell

Hyper-VDecember 24, 2024

In this post, we will introduce a PowerShell script that efficiently monitors the health of Hyper-V virtual machines in your environment. Regular health checks are vital for ensuring the performance and availability of your virtual infrastructure. This script will analyze the status of each virtual machine and provide a concise report, helping administrators proactively manage their resources. Here is the PowerShell script for monitoring Hyper-V virtual machine health:

powershell
# Import the Hyper-V module
Import-Module Hyper-V
# Get all Hyper-V virtual machines
$vms = Get-VM
# Create an array to hold health check results
$healthReport = @()
# Check each VM status and gather information
foreach ($vm in $vms) {
    $status = if ($vm.State -eq 'Running') { "Healthy" } else { "Critical" }
    $healthReport += [PSCustomObject]@{
        Name   = $vm.Name
        Status = $status
        State  = $vm.State
        MemoryUsageGB = [math]::Round($vm.Processor.UsagePercentage, 2)
    }
}
# Output health status in a formatted table
$healthReport | Format-Table -AutoSize
Write-Host "System health check completed for Hyper-V virtual machines."

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.