PowerCLI Script to Create/Delete in Bulk Snapshots

This one’s a quick tip I thought I’d share.

It’s a simple PowerCLI script I’ve used during daily operations, especially when working on multiple projects. It helped me create snapshots in bulk for multiple VMs — and with just a small tweak, it also helped delete them all in one go.


The PowerCLI Script:

You’ll need to update a few variables before running it:

  1. username – your vCenter username
  2. password – your vCenter password
  3. vCenter – the vCenter server you’re connecting to
  4. vmListPath – path to a .txt file containing the VM names (one per line)

Here’s the base script:

# Variables to update
$username = "your-username"
$password = "your-password"
$vCenter = "your-vcenter-name"
$vmListPath = "C:\Path\To\vm-list.txt"

Connect-VIServer -Server $vCenter -User $username -Password $password -ErrorAction Stop | out-null

$computers = Get-content $vmListPath

foreach($computer in $computers)
{

# VM information
$vm = $computer
$snapshotname = "Enter your Snapshot name"

# Take Snapshot
write-host "Creating snapshot [$snapshotname] for the VM [$vm]"
$snapstatus = New-Snapshot -vm $vm -name $snapshotname -confirm:$false -runasync:$true

}

If you want to delete those snapshots later with a one-liner, here you go:

foreach($computer in $computers)
{

Get-VM -Name $computer | Get-Snapshot | Remove-Snapshot -RunASync -Confirm:$false

}

Feel free to customize this script based on your specific reporting requirements and share your experiences or improvements in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *