← script library

Effortless Hyper-V Virtual Machine Management with PowerShell

Hyper-VDecember 24, 2024

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:

powershell
# 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

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.