← script library
Automated Virtual Machine Restart with PowerShell and Hyper-V
Hyper-VJanuary 10, 2025
In this post, we will introduce a handy PowerShell script designed to automate the restarting of Hyper-V virtual machines. This script allows administrators to efficiently manage VM availability and ensure that services are back online quickly after maintenance or unexpected shutdowns.
powershell
$vmName = "YourVMName" # Replace with the name of your Virtual Machine
$vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue
if (-not $vm) {
Write-Host "The virtual machine '$vmName' does not exist."
return
} elseif ($vm.State -eq 'Running') {
Write-Host "The virtual machine '$vmName' is currently running."
} else {
Write-Host "The virtual machine '$vmName' is not running. Attempting to start it..."
}
try {
Start-VM -Name $vmName
Write-Host "The virtual machine '$vmName' has been started successfully."
} catch {
Write-Host "ERROR: ""$($_.Exception.Message)"""
}
$vm.Refresh() # Refresh the VM object to get the latest state
Write-Host "Current state of '$vmName': $($vm.State)"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.
More in Hyper-V
Create a Hyper-V Checkpoint with PowerShell
2026-08-17Delete Old Hyper-V Checkpoints with PowerShell
2026-08-17Start a Hyper-V VM with PowerShell
2026-08-17Shut Down a Hyper-V VM with PowerShell
2026-08-17Efficient Hyper-V VM Status Reporting Script
2025-01-10Hyper-V Virtual Machine Snapshot Management Script
2025-01-10Ready when you are.
Try ServerEngine free for 7 days.