← script library

Automating Hyper-V Virtual Machine Checks with PowerShell

Hyper-VDecember 23, 2024

Maintaining the health and performance of your virtual machines is crucial for optimizing your IT infrastructure. This PowerShell script automates the process of checking the status of Hyper-V virtual machines. It retrieves essential information about each VM, including its current state, uptime, and resource usage. This script will: 1. List all Hyper-V virtual machines. 2. Check the status of each VM (running, stopped, etc.). 3. Display information like uptime and resource consumption. Using this script can significantly enhance your ability to monitor and manage virtual machines efficiently.

powershell
# Import the Hyper-V module
Import-Module Hyper-V
# Get all virtual machines
$vms = Get-VM
# Output header
Write-Host "=== Hyper-V Virtual Machine Status ==="
# Loop through each VM and retrieve information
foreach ($vm in $vms) {
    $status = $vm.State
    $uptime = (Get-Date) - $vm.Uptime
    $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 "Assigned Memory: $memoryAssigned"
    Write-Host "-------------------------------------"
}
Write-Host "VM status 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.