VMware Cloud Community
Vimal348
Enthusiast
Enthusiast
Jump to solution

Remove snapshots using script

Hello,

I have created snapshots for many VMs with the description:"Prior to install VM Hardware"

Can someone please tell me how to remove those snapshots that has a description : "Prior to install VM Hardware"

$vmlist = Get-Content "C:\VM\Snapshots\vmlists.txt"

foreach($VM in $VMlist) {

    Get-Snapshot -VM $vm |

    Remove-Snapshot -Confirm:$false

}

Disconnect-VIServer -Confirm:$false

Tags (2)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Then you could try like this

$vmlist = Get-Content "C:\VM\Snapshots\vmlists.txt"

foreach($VM in $VMlist) {

    Get-Snapshot -VM $vm | where{$_.Description -eq 'Prior to install VM Hardware'} |

    Remove-Snapshot -Confirm:$false

}

Disconnect-VIServer -Confirm:$false


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

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$vmlist = Get-Content "C:\VM\Snapshots\vmlists.txt"

foreach($VM in $VMlist) {

    Get-Snapshot -VM $vm -Name 'Prior to install VM Hardware' |

    Remove-Snapshot -Confirm:$false

}

Disconnect-VIServer -Confirm:$false


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

0 Kudos
Vimal348
Enthusiast
Enthusiast
Jump to solution

Hello LucD​,

Thank you for your script. Your suggestion works for the snapshot that has the name: 'Prior to install VM Hardware',

Here my snapshot names are different for each VMs, but Description is same : 'Prior to install VM Hardware',

Hence, is it possible to delete the snapshot collecting that Description name: 'Prior to install VM Hardware',

Upload and share screenshots and images - print screen online | Snipboard.io

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then you could try like this

$vmlist = Get-Content "C:\VM\Snapshots\vmlists.txt"

foreach($VM in $VMlist) {

    Get-Snapshot -VM $vm | where{$_.Description -eq 'Prior to install VM Hardware'} |

    Remove-Snapshot -Confirm:$false

}

Disconnect-VIServer -Confirm:$false


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

0 Kudos
Vimal348
Enthusiast
Enthusiast
Jump to solution

Thank you LucD

The script works perfectly...

0 Kudos
ansar2
Contributor
Contributor
Jump to solution

what is a general script i can use to delete snapshot from my environment?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What do you mean by "general"?
Remove all snapshots?


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

0 Kudos
Jocker-H
Contributor
Contributor
Jump to solution

Hello Luc,

Quick question please can we remove Snapshot from the listed VMs but based on Date and time of the snap wich should be listed  into the CVS file ? 

Exemple for the Csv VM Name / Date  / Time of Snap Creation / 

Thanks in advance

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The object returned by Get-Snapshot contains a property Created.
You could use a Where-clause to only select the snapshot corresponding with a specific DateTime.
Just be aware that specifying the correct time in your CSV might be inaccurate since a DateTime is very precise (sub-second).
I would approach this with a time interval based on the value in your CSV


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

ChristianCGI
Contributor
Contributor
Jump to solution

Hello,

For me I create a 3 lines script to remove all snapshot older then 3 days, but it looks like the log file not working, I get nothing in the file.

Do you have any suggestion ?

Connect-VIServer -server blablabla

$snapshot = Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-3)} | Out-File "C:\Temp\deletesnap\logs.txt" -Append
 
$snapshot | Remove-Snapshot -RemoveChildren -RunAsync -Confirm:$false | Out-File "C:\Temp\deletesnap\logs.txt" -Append
0 Kudos
LucD
Leadership
Leadership
Jump to solution

With the RunAsync switch the snapshot removal becomes a background task, and hence not producing any output.

You could use Get-Task, wait till all background tasks are completed, and then capture the info in the object returned by Get-Task.


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

0 Kudos
cool_breeze
Enthusiast
Enthusiast
Jump to solution

This is a good thread.

0 Kudos
Jocker-H
Contributor
Contributor
Jump to solution

Hello, could you please share with us the script after the modification. Im intrest in it.

 

Thanks

0 Kudos