← script library

PowerShell Script for VMware vSphere: VM Snapshot Management

VMware & vSphereJanuary 6, 2025

In this post, we are focusing on a PowerShell script that interacts with VMware vSphere to manage virtual machine (VM) snapshots. This script allows you to create a snapshot of a specified VM, ensuring that you have a restore point for your critical workloads. The following steps detail the process, and you can easily modify the script according to your needs.

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

# Connect to vCenter Server
$vcServer = "vcenter.domain.com"
$vcUsername = "username"
$vcPassword = "password"
$securePassword = ConvertTo-SecureString $vcPassword -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($vcUsername, $securePassword)
Connect-VIServer -Server $vcServer -Credential $cred

# Create a snapshot of the specified VM
$vmName = "YourVMName"
$snapshotDescription = "Snapshot taken on $(Get-Date)"
New-Snapshot -VM $vmName -Name "Snapshot-$(Get-Date -Format yyyy-MM-dd-HH-mm-ss)" -Description $snapshotDescription

# Verify the snapshot
Get-Snapshot -VM $vmName | Select-Object Name, Created, Description

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.