← script library

Efficient Virtual Machine Snapshot Management with PowerShell and Hyper-V

Hyper-VJanuary 10, 2025

In this post, we will explore a useful PowerShell script that helps manage Hyper-V virtual machine snapshots efficiently. This script allows you to create a snapshot of a specified virtual machine and provide feedback on the operation's status. This is particularly beneficial for system administrators who need to manage multiple VMs and ensure they have up-to-date backups.

powershell
$vmName = "YourVMName" # Replace with your VM name

if (-not (Get-VM -Name $vmName -ErrorAction SilentlyContinue)) {
    Write-Host "The virtual machine '$vmName' does not exist."
    return
}

$snapshotName = "$vmName-Snapshot-$(Get-Date -Format 'yyyyMMdd-HHmmss')"
try {
    Checkpoint-VM -Name $vmName -SnapshotName $snapshotName
    Write-Host "Snapshot '$snapshotName' created successfully for '$vmName'."
} catch {
    Write-Host "ERROR: ""$($_.Exception.Message)"""
}

$snapshots = Get-VM -Name $vmName | Get-VMSnapshot
if ($snapshots) {
    Write-Host "Current snapshots for '$vmName':"
    $snapshots | Format-Table -Property Name, CreationTime
} else {
    Write-Host "No snapshots found for '$vmName'."
}

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.