VMware Cloud Community
champ4
Contributor
Contributor

Help deleting all snapshots created in the past 2 weeks

Ok so Im still a pretty big newbie with VMware.

I have script that deletes anything older than two weeks but I need the opposite. Would this work?

Get-VM $VMName | Get-Snapshot | Where-object {$_.Created -gt (Get-Date).AddDays(-14)} | Remove-Snapshot

0 Kudos
3 Replies
Tsjo
Enthusiast
Enthusiast

Hi,

It seems it would work but  remove

| Remove-Snapshot

and see if you get the desired output.

If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
DZ1
Hot Shot
Hot Shot



I would run this first Get-VM | Get-Snapshot | where { ( (Get-date) - $_.created).days -gt 13 } | select Name, Created  That way you make sure that you are getting the correct snapshots.


If the results are what you want, I would remove the pipe for Select, and add Remove-Snapshot -confirm:false


I like getting the snapshots with the Get-Date - the $_.Created.days.  I saw it a while back in the book below.  It makes more sense in my head than using the AddDays(-number)  I don't work for any book publisher or anyone that would make money off of the book.  I did order it on Amazon.


VMware vSphere PowerCLI Reference: Automating vSphere Administration


0 Kudos
kunaludapi
Expert
Expert

Get-VM $VMName | Get-Snapshot | Where-object {$_.Created -lt (Get-Date).AddDays(-14)} | Remove-Snapshot

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos