Close Menu
PowerShell.roPowerShell.ro
  • Home
  • Automation
  • Powershell
  • VMWare
  • Windows
LinkedIn X (Twitter) Facebook
PowerShell.roPowerShell.ro
  • Home
  • Automation
  • Powershell
  • VMWare
  • Windows
LinkedIn Facebook X (Twitter)
PowerShell.roPowerShell.ro
Home»Automation»Remove Snapshots script
Automation

Remove Snapshots script

Catalin CristescuBy Catalin CristescuJune 27, 2023
Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
Share
Facebook Twitter LinkedIn Pinterest Email

One approach to selectively delete snapshots in a vCenter, specifically targeting those created by specific individuals, involves utilizing a ‘TAG’ system applied to either the snapshot name or description. This method allows for the identification and subsequent deletion of snapshots based on the specified criteria, rather than removing all snapshots across the vCenter environment.

Usage:

.\remove-snapshots.ps1 -vcenter vcsa.local -username * -password * -tag “autodelete”

Script

#requires -modules VMware.VimAutomation.Core

param (
    [CmdletBinding()]
    [Parameter(Mandatory = $true)]
    [string]$VCenter,
     
    [Parameter(Mandatory = $true)]
    [string]$Username,
     
    [Parameter(Mandatory = $true)]
    [string]$Password,

    [Parameter(Mandatory = $true)]
    [string]$tag
)
 
Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Set-PowerCLIConfiguration -ProxyPolicy NoProxy -DefaultVIServerMode multiple -Scope User -InvalidCertificateAction ignore -Confirm:$false | Out-Null
 
# Connection to vCenter
try {
    Connect-VIServer -Server $VCenter -User $Username -Password $Password -ErrorAction Stop | Out-Null
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidLogin] {
    Write-Host "Cannot connect to $VCenter with provided credentials" -ForegroundColor Red
    Continue
}
catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViServerConnectionException] {
    Write-Host "Cannot connect to $VCenter - check IP/FQDN" -ForegroundColor Red
    Continue
}
catch {
    Write-Host "Cannot connect to $VCenter - Unknown error" -ForegroundColor Red
    Continue
}

$limit= (Get-Date).AddDays(-10)

Get-VM | Sort | Get-Snapshot | Where-Object {($_.Created -lt $limit) -and ($_.Name -like "*$($tag)*")} | Remove-Snapshot -RunAsync -Confirm:$false
Get-VM | Sort | Get-Snapshot | Where-Object {($_.Created -lt $limit) -and ($_.Description -like "*$($tag)*")} | Remove-Snapshot -RunAsync -Confirm:$false

Disconnect-VIserver -Server $vcu -Confirm:$false
 
autodelete powercli powershell remove snapshot snapshots
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticlePowerCLI – increase C drive on Windows server
Next Article PowerCLI: Get Storage Paths of ESXi servers from vCenter
Catalin Cristescu
  • Website

Related Posts

Automation

PowerShell script for automated remediation for CrowdStrike issue

July 20, 2024
VMWare

vExperts stats 2024

May 27, 2024
Automation

Tagging Virtual Machines with the CISTag Module in PowerShell

February 1, 2024

Comments are closed.

Recent Posts
  • PowerShell script for automated remediation for CrowdStrike issue
  • vExperts stats 2024
  • Tagging Virtual Machines with the CISTag Module in PowerShell
  • Update HPE ILO firmware using PowerShell
  • PowerCLI script to check Cluster Overcommitment ratio
Categories
  • Automation (11)
  • Powershell (14)
  • Uncategorized (1)
  • VMWare (15)
  • Windows (2)
About Powershell.ro

Powershell.ro, the ultimate hub for tech enthusiasts! Dive into expert-written articles on PowerShell scripts, VMware, automation, Microsoft technologies, and DevOps practices. Unlock tips, tutorials, and solutions designed to empower IT professionals and boost your skills in automation and infrastructure management.

Recent Posts
  • PowerShell script for automated remediation for CrowdStrike issue
  • vExperts stats 2024
  • Tagging Virtual Machines with the CISTag Module in PowerShell
  • Update HPE ILO firmware using PowerShell
  • PowerCLI script to check Cluster Overcommitment ratio
LinkedIn
© 2025 PowerShell.ro.

Type above and press Enter to search. Press Esc to cancel.