PowerCLI Scripting - SnapShot Create Date is older than 3 days

Hi All!

I was tasked to create a script which queries all the snapshots created VMs in an eSXI host.   The main requirement is to generate a list of snapshots and VMs with create date of older than 3 days.   If there is a snapshot older than 3 days, create a ticket (internal ticketing system) - what I did was to send an email instead that will trigger the ticket creation.

I'm new in scripting and doing my best to have the most simple script to get this information.   From my research over the internet (here‌ and some tweaking on SMTP config) I was able to use a script to get the list of snapshots and their creation date, and send the report via email.   I now need to trim down the report to those snapshots which are older than 3 days and send an email to me.

Can anyone clarify what I need to only include the snapshots with a create date greater than 3 days?

Below is the script I'm using

# PowerShell script to check for VM snapshots and send Email report

add-pssnapin VMware.VimAutomation.Core

Connect-VIServer -Server <ip-or-host> -User <username> -Password <password>

# HTML formatting

$a = "<style>"

$a = $a + "BODY{background-color:white;}"

$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"

$a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: LightBlue}"

$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: white}"

$a = $a + "</style>"

# Main section of check

Write-Host "Checking VMs for for snapshots"

$date = get-date

$datefile = get-date -uformat '%m-%d-%Y-%H%M%S'

$filename = "c:\dir\Snapshots_" + $datefile + ".htm"

# Get list of VMs with snapshots

# Note:  It may take some time for the  Get-VM cmdlet to enumerate VMs in larger environments

$ss = Get-vm | Get-Snapshot

Write-Host "   Complete " -ForegroundColor Green

Write-Host "Generating VM snapshot report"

#$ss | Select-Object vm, name, SizeGB, Created, powerstate | ConvertTo-HTML -head $a -body "<H2>VM Snapshot Report</H2>"| Out-File $filename

$ss | Select-Object vm, name, SizeGB, Created, powerstate | ConvertTo-HTML -head $a -body "<H2>VM Snapshot Report </H2>"| Out-File $filename

Write-Host "   Complete " -ForegroundColor Green

Write-Host "Your snapshot report has been saved to:" $filename

$SMTPServer = <smtp_server>

$SMTPPort = 587

$Username = "email@email.com"

#Define the receiver of the report

$to = "recipient.email@email.com"

$subject = "VM Snapshot Report"

$body = "VM Snapshot Report"

$attachment = new-object Net.Mail.Attachment($filename)

$message = New-Object System.Net.Mail.MailMessage

$message.subject = $subject

$message.body = $body


$message.from = $username


$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);

$smtp.EnableSSL = $true

#$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);


write-host "Mail Sent"

4 Replies


Change this

$ss = Get-vm | Get-Snapshot


$ss = Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-3)}

Thanks n Regards
Umesh Ahuja

If your query resolved then please consider awarding points by correct or helpful marking.

Great thanks!

Yeah, it works!

0 Kudos

Hi Umesh, Similarly can you help with the code for getting  the details of the snapshots which are above or equal to 3000 GB please. 

0 Kudos

You could also reply in your original thread power shell script to check a VM snapshot size whe... - VMware Technology Network VMTN

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

0 Kudos