Aboodnet
Aboodnet

Reputation: 143

How to delete a specific snapshot using Powershell in Hyper-V

Dears,

I am trying to delete a specific snapshot for one of our VMs but all the snapshots are being deleted instead.

PS C:\Users\abood>  Get-VMSnapshot -VMName KUW-HV01

VMName   Name SnapshotType CreationTime         ParentSnapshotName
------   ---- ------------ ------------         ------------------
KUW-HV01 OLD  Standard     7/22/2020 9:17:48 PM                   
KUW-HV01 NEW  Standard     7/22/2020 9:18:08 PM OLD  

PS C:\Users\abood>  Remove-VMSnapshot -VMName KUW-HV01 -WhatIf | Where-Object {$_.Name -eq "NEW"}
What if: Remove-VMSnapshot will remove snapshot "NEW".
What if: Remove-VMSnapshot will remove snapshot "OLD".

How can i delete only "NEW" or "OLD" while keeping the other one?

Thanks in advance,

Upvotes: 0

Views: 1615

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36297

Most cmdlets that take an action against something (such as Remove-VMSnapshot) will allow you to pipe objects into them to specify what objects to take that action against. For example, you already used Get-VMSnapshot to get the two snapshots of that specific VM. You can then use Where-Object to specify only the snapshot you want to delete and filter out any that you want to keep like this:

Get-VMSnapshot -VMName KUW-HV01 | Where-Object {$_.Name -eq "NEW"}

VMName   Name SnapshotType CreationTime         ParentSnapshotName
------   ---- ------------ ------------         ------------------
KUW-HV01 NEW  Standard     7/22/2020 9:18:08 PM OLD  

Then you pipe that to Remove-VMSnapshot to specify exactly what you want to remove.

Get-VMSnapshot -VMName KUW-HV01 | Where-Object {$_.Name -eq "NEW"} | Remove-VMSnapshot -WhatIf

That should result in this:

What if: Remove-VMSnapshot will remove snapshot "NEW".

Upvotes: 1

Related Questions