← script library

Automate VM Snapshot Management in VMware vSphere

VMware & vSphereJanuary 7, 2025

In this post, we will create a PowerShell script that automates the process of managing snapshots for your virtual machines in VMware vSphere.

powershell
# Import the VMware PowerCLI module
Import-Module VMware.PowerCLI

# Connect to vCenter Server
$vcServer = "your_vcenter_server"
$vcUsername = "your_username"
$vcPassword = "your_password"
Connect-VIServer -Server $vcServer -User $vcUsername -Password $vcPassword

# Get all VMs in the vCenter
$vms = Get-VM

# Create snapshots for each VM
foreach ($vm in $vms) {
    New-Snapshot -VM $vm -Name "AutoSnapshot-$(Get-Date -Format yyyyMMdd-HHmmss)" -Description "Automated snapshot created on $(Get-Date)"
}

# Disconnect from vCenter Server
Disconnect-VIServer -Server $vcServer -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.