Effortless Hyper-V Virtual Machine Management with PowerShell

In this post, we will share a PowerShell script designed to simplify Hyper-V virtual machine management. Efficient management of virtual machines is crucial for maintaining performance and ensuring resources are utilized effectively. This script provides capabilities to quickly start, stop, and check the status of virtual machines, making it an essential tool for IT administrators managing a Hyper-V environment.
Here is the PowerShell script for managing Hyper-V virtual machines:

# Define the name of the virtual machine
$vmName = "YourVMName"
# Check the status of the virtual machine
$vmStatus = Get-VM -Name $vmName
if ($vmStatus -eq $null) {
    Write-Host "Virtual machine '$vmName' does not exist."
} else {
    Write-Host "Current status of VM '$vmName': $($vmStatus.State)"
    # Start the VM if it is not running
    if ($vmStatus.State -ne 'Running') {
        Start-VM -Name $vmName
        Write-Host "Virtual machine '$vmName' has been started."
    }
    # Stop the VM if it is running
    if ($vmStatus.State -eq 'Running') {
        Stop-VM -Name $vmName -Force
        Write-Host "Virtual machine '$vmName' has been stopped."
    }
}
# List all VMs and their statuses
Get-VM | Select-Object Name, State | Format-Table -AutoSize