Using PowerShell to Remove Virtual Machine Snapshots in VMware ESXi 4.1

Hi All,

VMware snapshots can be a very good insurance policy when making changes to a VM. Its also important to make sure not to leave old snapshots laying around as this will cause you problems later. To get a report of snapshots using powershell please see my other blog Using PowerShell to get a list Virtual Machine Snapshots in VMware ESXi 4.1 . In this blog I will explain how to remove Virtual Machine snapshots using PowerShell. We will be using the “Remove-Snapshot” cmdlet and piping it to the “Get-Snapshot” in the examples below. To do anything with PowerShell in VMware you will need to download and install VMware vSphere PowerCLI. You will then need to open PowerCLI connect to your VMWare vCenter server by typing “Connect-VIServer vCenterServerName”.

To view the snapshots for a Virtual Machine before you remove them, you can type the following command replacing VMname with the name of your Virtual Machine:

Get-Snapshot VMname | select name,id

In the command above, I select to see the name and id which will give you a list of the servers snapshot names and snapshot IDs. The ID will be important to note when being asked to confirm the removal of the snapshot when running the “Remove-Snapshot” cmdlet.

To remove the snapshots found in the command above, we can use the “Get-Snapshot” cmdlet and pipe it to the “Remove-Snapshot” cmdlet as shown below, replacing VMname with the name of your Virtual Machine (This will remove all snapshots for the specified Virtual Machine):

Get-Snapshot VMname | Remove-Snapshot

You will then be asked to confirm the deletion of the snapshot (you will see that the confirmation uses the ID that we selected to view earlier). Type “Y” and press enter to confirm the removal. If you did not want to be asked to confirm the removal you could use the “confirm” parameter as shown on the command below:

Get-Snapshot VMname | Remove-Snapshot -confirm:$false

If you wanted to see what happens by typing the command above without having any snapshots removed, the “Remove-Snapshot” cmdlet supports the use of the “whatif” parameter. You could type the following to see what will happen without actually removing anything:

Get-Snapshot VMname | Remove-Snapshot –whatif

If you wanted to remove snapshots for multiple VMs and you had your Virtual Machines organized in folders, you could type the following command to remove all snapshots for all Virtual Machines located in the “My Lab” folder:

Get-VM -location “My Lab” | Get-Snapshot | Remove-Snapshot -confirm:$false

In the above command, we use the “Get-VM” cmdlet to get all of the Virtual machines in the “My Lab” folder. We then pipe it to the “Get-Snapshot” cmdlet to get the snapshots for the Virtual machines in the “My Lab” folder. Next we pipe this to the “Remove-Snapshot” cmdlet to remove all snapshots found on Virtual Machines in the “My Lab” folder.

If you wanted to remove all VM snapshots regardless of folder location, you could remove the -location parameter from the command in the last example. This will remove all snapshots for VMs that have them. The snapshots will be removed from each VM one by one. The command is as follows:

Get-VM | Get-Snapshot | Remove-Snapshot -confirm:$false

I hope this helps. If you have any questions or feedback, please leave a comment. Thanks.

Related Links:

Using PowerShell to get a list Virtual Machine Snapshots in VMware ESXi 4.1

Using PowerShell to Gracefully Shut Down Multiple Virtual Machines in VMware

Using PowerShell to create a Virtual Machine Inventory in VMware and Export it to a CSV File

Using PowerShell to Create Virtual Machine Snapshots in VMware

Using PowerShell to View  Virtual Machine Disks (VMDK) Information in VMware and Export to a CSV File

 

Share Button
Author: BC

BC is a Pipe2text.com contributor and has been an IT Professional for 15 years. He currently works as a Sr. Systems Engineer at a private firm. He enjoys working with Active Directory, Exchange and VMware among other technologies.


2 Responses to Using PowerShell to Remove Virtual Machine Snapshots in VMware ESXi 4.1

  1. Praveen says:

    Hi ,

    Very Use full.
    But I have doubt. How to delete a Particular snapshot among many snapshots of a VM.
    Ex : I have 10 Snapshots of a VM, and I want to delete 7th Snapshot only ( not all ) how can I do that? Please explain. Thanks in advance.

    Regards,
    Praveen

  2. BC says:

    Lets say you have 2 snapshots. Snapshot1 and Snapshot2. You want to remove snapshot2. You can try this:
    Get-VM vmname | Get-Snapshot -Name snapshot2 | Remove-Snapshot

Leave a Reply

Your email address will not be published. Required fields are marked *