← script library

Monitoring System Health Checks in Hyper-V with PowerShell

Hyper-VJanuary 4, 2025

In this post, we will focus on a PowerShell script that automates system health checks for virtual machines running in Hyper-V. Regular health checks are vital for ensuring that your virtual environments are functioning correctly and efficiently. This script will help you monitor CPU usage, memory allocation, and disk space for each virtual machine, allowing you to identify potential issues before they impact your operations.

powershell
# Define the Hyper-V host
$hyperVHost = "localhost"  # You may replace with the hostname or IP address of your Hyper-V host

# Get all virtual machines from the Hyper-V host
$vms = Get-VM -ComputerName $hyperVHost
Write-Host "Found $($vms.Count) virtual machines on host $hyperVHost."

# Check CPU usage for each virtual machine
foreach ($vm in $vms) {
    $cpuUsage = Get-VMProcessor -VM $vm
    Write-Host "$($vm.Name) CPU Usage: $($cpuUsage.LoadPercentage)%"
}

# Check Memory usage for each virtual machine
foreach ($vm in $vms) {
    $memory = Get-VMMemory -VM $vm
    Write-Host "$($vm.Name) Memory Assigned: $($memory.MemoryStartup) MB - Memory Demand: $($memory.MemoryDemand) MB"
}

# Check Disk space for each virtual machine
foreach ($vm in $vms) {
    $hardDisks = Get-VMHardDiskDrive -VM $vm
    foreach ($disk in $hardDisks) {
        $diskSize = Get-VHD -Path $disk.Path
        Write-Host "$($vm.Name) Disk: $($disk.Path) - Size: $([math]::round($diskSize.FileSize/1GB, 2)) GB - Used: $([math]::round($diskSize.Size/1GB, 2)) GB"
    }
}

# Summary output
Write-Host "Health checks completed for virtual machines on host $hyperVHost."
Write-Host "Please ensure that you monitor any instances where usage exceeds expected thresholds."

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.