← script library

Hyper-V VM Resource Usage Monitor

Hyper-VJanuary 10, 2025

In this post, we are introducing a PowerShell script that allows you to monitor the resource usage of your Hyper-V virtual machines. This script provides valuable insights into the memory and CPU utilization of each VM, enabling you to optimize your virtual environment efficiently.

powershell
$virtualMachines = Get-VM

foreach ($vm in $virtualMachines) {
    $cpuUsage = Get-VMProcessor -VM $vm.Name | Measure-Object -Property CPUUsage -Average | Select-Object -ExpandProperty Average
    $memoryUsage = Get-VMMemory -VM $vm.Name | Measure-Object -Property MemoryAssigned -Sum | Select-Object -ExpandProperty Sum
    Write-Host "VM Name: $($vm.Name) - CPU Usage: $cpuUsage% - Memory Usage: $memoryUsage MB"
}

$cpuThreshold = 80
$memoryThreshold = 2048
$highUsageVMs = $virtualMachines | Where-Object {
    ($_.CPUUsage -gt $cpuThreshold) -or ($_.MemoryAssigned -gt $memoryThreshold)
}
foreach ($vm in $highUsageVMs) {
    Write-Host "High Resource Usage VM: $($vm.Name)"
}

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.