VMW_RemoveOldSnapshots.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 897cb0d5-76d2-41e7-96ef-f2ca7eb5a143
 
.AUTHOR Andrew Anderson - Twitter @drewjanderson
 
.COMPANYNAME AndersonTech
 
.COPYRIGHT
 
.TAGS PowerCLI VMware Snapshots
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES PowerCLI
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
Tested and now ready for release
 
#>


<#
 
.DESCRIPTION
 This script removes VMware VM snapshots that are older than 14 days
 
#>
 

# Script Variables
$todaysdate = Get-Date -UFormat '%Y-%m-%d'
$deleteolderthan = '14'
$viservername = @('vcenter1', 'vcenter2')

# Imports PowerCLI Module
Import-Module VMware.VimAutomation.Core

# Connects to the vCenter or ESXi servers
Connect-VIServer $viservername

# Collects VM snapshot information for all VMs where the snapshots are older than days specified
$snapshots = Get-VM | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date).AddDays(-$deleteolderthan)}

# Removes snapshots older than days specified
$snapshots | Remove-Snapshot -RemoveChildren -RunAsync -Confirm:$false

# Disconnects from the connected vCenter or ESXi servers
Disconnect-VIServer $viservername -Confirm:$false

# Unloads the PowerCLI module
Remove-Module VMware.VimAutomation.Core