← script library

Automate VM Snapshot Management in VMware ESXi with PowerShell

VMware & vSphereJanuary 6, 2025

In this post, we present a PowerShell script that automates the creation and deletion of snapshots for your virtual machines. Snapshots are vital for backup and recovery processes, and managing them efficiently is essential for system administrators.

powershell
# Load VMware PowerCLI module
Import-Module VMware.PowerCLI -ErrorAction Stop

# Define connection details
$esxiHost = "<ESXi_Host>"
$username = "<Your_Username>"
$password = "<Your_Password>"
# Convert password to a secure string
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ($username, $securePassword)
# Connect to the ESXi host
Connect-VIServer -Server $esxiHost -Credential $credentials -ErrorAction Stop

# Define the VM and snapshot details
$vmName = "<VM_Name>"
$snapshotDescription = "Automated snapshot created on $(Get-Date -Format yyyy-MM-dd HH:mm:ss)"
# Create a snapshot for the VM
New-Snapshot -VM $vmName -Name "Snapshot-$(Get-Date -Format yyyyMMddHHmmss)" -Description $snapshotDescription -Confirm:$false
Write-Host "Snapshot created for VM: $vmName"

# List all snapshots for the VM
$snapshots = Get-Snapshot -VM $vmName
Write-Host "Snapshots for VM: $vmName"
foreach ($snapshot in $snapshots) {
    Write-Host " - $($snapshot.Name) created on $($snapshot.Created)"
}

# Disconnect from the ESXi host
Disconnect-VIServer -Server $esxiHost -Confirm:$false

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.