← script library
Monitoring VM Resource Usage in VMware vSphere with PowerShell
VMware & vSphereJanuary 6, 2025
Keeping track of resource usage for your virtual machines in VMware vSphere is vital for efficient resource allocation and performance optimization. This PowerShell script provides a way to gather CPU and memory usage statistics for specified VMs, helping you monitor their performance effectively.
powershell
# Load VMware PowerCLI module
Import-Module VMware.PowerCLI
# Connect to vSphere server
$vCenterServer = "your_vcenter_server"
$username = "your_username"
$password = "your_password"
Connect-VIServer -Server $vCenterServer -User $username -Password $password
# Define virtual machines to monitor
$vmNames = @("VM1", "VM2", "VM3") # Substitute with your VM names
# Gather and display CPU and memory usage
foreach ($vmName in $vmNames) {
$vm = Get-VM -Name $vmName
if ($null -ne $vm) {
$cpuUsage = Get-Stat -Entity $vm -Stat "cpu.usage.average" -Realtime | Measure-Object -Property Value -Average | Select-Object -ExpandProperty Average
$memUsage = Get-Stat -Entity $vm -Stat "mem.usage.average" -Realtime | Measure-Object -Property Value -Average | Select-Object -ExpandProperty Average
Write-Host "VM: $vmName, CPU Usage: $cpuUsage%, Memory Usage: $memUsage%"
} else {
Write-Host "VM $vmName not found."
}
}
# Disconnect from vSphere
Disconnect-VIServer -Server $vCenterServer -Confirm:$falseRun 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.
More in VMware & vSphere
Ready when you are.
Try ServerEngine free for 7 days.