← script library
Automating VM Snapshots in VMware vSphere Using PowerShell
VMware & vSphereJanuary 6, 2025
In this post, I will share a useful PowerShell script that automates the process of creating snapshots for virtual machines (VMs) in VMware vSphere. Snapshots are vital for backup and recovery, and having an automated solution can save you a lot of time. This script connects to your vSphere server, identifies VMs that are powered on, and creates a snapshot for each VM. Before diving into the script, make sure you have the VMware PowerCLI installed and configured on your system.
powershell
# Install PowerCLI if not already installed
Install-Module -Name VMware.PowerCLI -AllowClobber -Force
# Connect to the vSphere server
$server = "your_vcenter_server"
$username = "username"
$password = "password"
Connect-VIServer -Server $server -User $username -Password $password
# Get all powered-on VMs
$vmList = Get-VM | Where-Object { $_.PowerState -eq "PoweredOn" }
# Create snapshots for each VM
foreach ($vm in $vmList) {
$snapshotName = "Snapshot_" + (Get-Date -Format "yyyyMMdd_HHmmss")
New-Snapshot -VM $vm -Name $snapshotName -Description "Automated snapshot created on $($snapshotName)"
Write-Host "Created snapshot $snapshotName for VM $($vm.Name)"
}
# Disconnect from the vSphere server
Disconnect-VIServer -Server $server -Confirm:$falseRun 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 VMware & vSphere
Ready when you are.
Try ServerEngine free for 7 days.