VMware Cloud Community
thisischristia1
Contributor
Contributor

Delete-Snapshot Created on Specific Date from a list of VMs

How would i do this? I am creating snapshots just incase I need them. I want to easy way to delete these from the list of VMs I have.

Any ideas?

0 Kudos
12 Replies
LucD
Leadership
Leadership

In it's simplest form, you could do something like this.

It assumes you have the names of the VMs in a .txt file, one name per line.

$vmNames = Get-Content -Path .\vmnames.txt

Get-VM -Name $vmNames | Get-Snapshot | Remove-Snapshot -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
thisischristia1
Contributor
Contributor

Thanks i have this simple piece of code already. Im looking to specify a date so that i dont have to worry about deleting the wrong snapshot.

0 Kudos
thisischristia1
Contributor
Contributor

Or if i wanted to delete the snapshots based on what's in the title such as -like *NAMEHERE* ?

0 Kudos
LucD
Leadership
Leadership

If you want to limit this to only snapshots created on a specific date, you could do.

Note that the format in which you supply the date (dd/mm/yy or mm/dd/yy) depends on the regional settings on the station.

$targetDate = '11/07/19'

$date = Get-Date $targetDate

$vmNames = Get-Content -Path .\vmnames.txt


Get-VM -Name $vmNames | Get-Snapshot |

where{$_.Created.Date -eq $date} |

Remove-Snapshot -Confirm:$false


When you want to limit the removal to snapshots with a specific text in the Description, you could do

$tgtText = 'Some text'

$vmNames = Get-Content -Path .\vmnames.txt


Get-VM -Name $vmNames | Get-Snapshot |

where{$_.Description -match $tgtText} |

Remove-Snapshot -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
thisischristia1
Contributor
Contributor

Tried this code, but it doesnt delete. Tried modifying it to " -like " instead of match and that doesnt work either.

any ideas?

0 Kudos
LucD
Leadership
Leadership

What text did you specify, and what is in the Description field of the snapshot?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
thisischristia1
Contributor
Contributor

Here is what i have:

$tgtText = 'August'

$vmNames = Get-Content -Path "FILEPATH\DeleteSnapShots.txt"

Get-VM -Name $vmNames | Get-Snapshot | where{$_.Description -like $tgtText} | Remove-Snapshot -Confirm:$false

Here's whats in vCenter on the VM I added to the list below:

Id                : VirtualMachineSnapshot-snapshot-197033

Name              : August patches - VM Snapshot 8%252f22%252f2019, 9:20:40 AM

0 Kudos
LucD
Leadership
Leadership

I don't see any reason why the -match operator wouldn't work in that case.

$tgtText = 'August'

$vmNames = Get-Content -Path "FILEPATH\DeleteSnapShots.txt"

Get-VM -Name $vmNames | Get-Snapshot | where { $_.Description -match $tgtText }

If you insist on using the -like operator, which has fewer options compared to the -match operator, you will have to add meta-characters to the string.

Something like this

$tgtText = 'August'

$vmNames = Get-Content -Path "FILEPATH\DeleteSnapShots.txt"

Get-VM -Name $vmNames | Get-Snapshot | where { $_.Description -like "*$tgtText*" }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
thisischristia1
Contributor
Contributor

Tried it again this morning. Still wont work. Doesnt delete the snapshot.

I tried placing the VM name inline and it still wont delete the snapshot.

0 Kudos
LucD
Leadership
Leadership

That would mean that the following does not list any snapshots?

$tgtText = 'Some text'

$vmNames = Get-Content -Path .\vmnames.txt

Get-VM -Name $vmNames | Get-Snapshot |

where{$_.Description -match $tgtText}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
thisischristia1
Contributor
Contributor

there are 2 snapshots on this VM. If the name was spelled incorrectly I would see an error. I think the pipe is not working.

0 Kudos
LucD
Leadership
Leadership

How do you run this snippet?
From a .ps1 file, or do you copy the lines to the PS prompt?
Could you attach the .ps1 file or share a screenshot?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos