Automating Hyper-V Virtual Machine Checks with PowerShell

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.

# 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."