← script library
Automate VMware vSphere VM Snapshot Creation with PowerShell
VMware & vSphereJanuary 6, 2025
In this post, we will explore a PowerShell script designed to automate the creation of virtual machine snapshots in VMware vSphere. Snapshots can be a crucial part of managing your virtual environments, allowing you to capture the state of a VM at a specific point in time. This script will make it easy to create snapshots across multiple VMs quickly, all while integrating seamlessly with your existing VMware infrastructure.
powershell
# Importing the VMware PowerCLI module
Import-Module VMware.PowerCLI
# Connecting to the vCenter Server
$vcServer = "vcenter-server"
$vcUser = "your-username"
$vcPassword = "your-password"
Connect-VIServer -Server $vcServer -User $vcUser -Password $vcPassword
# Define a list of VMs to snapshot
$vmNames = @("VM1", "VM2", "VM3")
# Loop through each VM and create a snapshot
foreach ($vmName in $vmNames) {
$vm = Get-VM -Name $vmName
if ($vm) {
New-Snapshot -VM $vm -Name "Snapshot-$(Get-Date -Format yyyyMMdd-HHmmss)" -Description "Automated snapshot created on $(Get-Date)"
Write-Host "Snapshot created for VM: $vmName"
} else {
Write-Host "VM not found: $vmName"
}
}
# Disconnecting from the vCenter Server
Disconnect-VIServer -Server $vcServer -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.