Code / PowerCLI · April 10, 2024

VMware Snapshot Management with PowerCLI: A Guide to Listing and Deleting Snapshots

Snapshots are a vital feature in VMware environments, offering a safety net for virtual machines during upgrades, testing, and troubleshooting. However, unmanaged snapshots can lead to significant storage issues, increased latency, and even impact virtual machine performance. To maintain a clean and efficient environment, administrators need tools to manage snapshots effectively.

In this blog, we’ll explore two PowerCLI scripts designed to streamline snapshot management. The first script lists all snapshots across your vCenter environment and exports the data into a CSV file for reporting and auditing purposes. The second script takes it a step further by identifying and optionally deleting snapshots older than three days, providing a safeguard against unnecessary storage usage.

 

1. Script to List All Snapshots

This script retrieves detailed information about all snapshots in your vCenter environment, including the virtual machine name, snapshot name, creation date, size, and description. The data is then exported to a CSV file for easy analysis and record-keeping.

Key steps in the script:

  • Connect to your vCenter server using secure credentials.
  • Retrieve snapshot data using Get-VM and Get-Snapshot cmdlets.
  • Format the output as a CSV for a clear and organized report.

With this script, administrators gain visibility into their snapshot inventory, helping them identify unused or outdated snapshots that might be consuming valuable storage resources.


2. Script to Delete Snapshots Older Than Three Days

Old snapshots can quickly accumulate and cause storage inefficiencies. This script automates the process of identifying and deleting snapshots older than three days, ensuring a lean and optimized vSphere environment.

Key features of the script:

  • Define a threshold date to filter snapshots older than three days.
  • Present a list of eligible snapshots to the user for review.
  • Prompt the user for confirmation before proceeding with deletion.

This cautious approach ensures that administrators remain in control of snapshot deletions, reducing the risk of unintended data loss.


Why Automate Snapshot Management?

Manually managing snapshots can be a time-consuming and error-prone task, especially in large environments. Automating the process with PowerCLI not only saves time but also ensures consistency and accuracy in snapshot management. By leveraging these scripts, administrators can:

  • Maintain a clear overview of snapshot usage.
  • Prevent unnecessary storage bloat.
  • Ensure compliance with organizational policies for snapshot retention.

How to Use These Scripts

  1. Customize the scripts with your vCenter credentials and desired output paths.
  2. Execute the scripts in a PowerCLI session with appropriate permissions.
  3. Regularly run the listing script to monitor snapshot usage and leverage the deletion script for routine cleanup.

By incorporating these scripts into your workflow, you’ll be able to manage snapshots efficiently, optimize your VMware environment, and avoid the pitfalls of snapshot sprawl.


 

 

Script to list the snapshots:

# Ignore certificate warnings
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

# Define vCenter credentials
$vCenterServer = “your_vcenter_server”
$username = “your_username”
$password = “your_password”

# Connect to vCenter
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $securePassword)
Connect-VIServer -Server $vCenterServer -Credential $cred

# Find all snapshots and store in an array for exporting
$snapshotData = Get-VM | Get-Snapshot | ForEach-Object {
[PSCustomObject]@{
VMName = $_.VM.Name
SnapshotName = $_.Name
Created = $_.Created
SizeMB = $_.SizeMB
Description = $_.Description
}
}

# Export to CSV
$outputPath = “C:\Path\To\Output\SnapshotsReport.csv”
$snapshotData | Export-Csv -Path $outputPath -NoTypeInformation -UseCulture

Write-Output “Snapshot data has been exported to $outputPath.”

# Disconnect from vCenter
Disconnect-VIServer -Server $vCenterServer -Confirm:$false

 

 

Script to list the snapshots and delete 3 days old snapshots with a confirmation question:

# Ignore certificate warnings
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

# Define vCenter credentials
$vCenterServer = “your_vcenter_server”
$username = “your_username”
$password = “your_password”

# Connect to vCenter
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $securePassword)
Connect-VIServer -Server $vCenterServer -Credential $cred

# Define snapshot age threshold (3 days)
$thresholdDate = (Get-Date).AddDays(-3)

# Find snapshots older than 3 days
$snapshotsToDelete = Get-VM | Get-Snapshot | Where-Object { $_.Created -lt $thresholdDate }

# Check if there are any snapshots to delete
if ($snapshotsToDelete.Count -eq 0) {
Write-Output “No snapshots older than 3 days were found.”
} else {
# Display snapshots and ask for confirmation
Write-Output “The following snapshots are older than 3 days and will be deleted:”
$snapshotsToDelete | ForEach-Object {
Write-Output “VM Name: $($_.VM.Name), Snapshot Name: $($_.Name), Created: $($_.Created)”
}

# Ask for confirmation
$confirmation = Read-Host “Do you want to delete these snapshots? (yes/no)”

if ($confirmation -eq “yes”) {
# Delete snapshots
$snapshotsToDelete | ForEach-Object {
Write-Output “Deleting snapshot ‘$($_.Name)’ for VM ‘$($_.VM.Name)’…”
$_ | Remove-Snapshot -Confirm:$false
}
Write-Output “Snapshots deleted successfully.”
} else {
Write-Output “Snapshot deletion canceled.”
}
}

# Disconnect from vCenter
Disconnect-VIServer -Server $vCenterServer -Confirm:$false