PowerShell sends daily successful delivery mail report
Every Exchange administrator will encounter the need to monitor the daily mail queue: how many emails are sent, how many emails are received, and how many emails are blocked by policies and anti-spam. However, it is inconvenient to display every query in the Exchange Management Shell window. In order to better monitor the delivered emails every day, here is a script for mail delivery report.
#Add Exchange Snap-in
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
#Define administrative credentials
$Password = "12345678" | ConvertTo-SecureString -asPlainText -Force
$Username = "Domain\User"
$Credential = New-Object System.Management.Automation.PSCredential($username,$password)
#Define generated CSV path
$File = "C:\Scripts\Exchange\$(Get-Date -Format 'yyyy-MM-dd').csv"
#Filter delivery logs from 0:00 the day before to 0:00 today and store them in the Deliver variable
$Deliver = Get-MailboxServer | Get-MessageTrackingLog -Start (Get-Date).Date.AddDays(-1) -End (Get-Date).Date -EventId Deliver -ResultSize unlimited
#Select specific attributes and sort by topic and output to CSV
$Deliver | Select-Object Source,Sender,{$_.Recipients},MessageSubject | Sort-Object MessageSubject -Descending | Export-Csv -Path $File -NoClobber -NoTypeInformation -Encoding UTF8
#Send Mail Reports
Send-MailMessage -From User@domain.com -To User2@domain.com -SmtpServer mail.domain.com -Credential $Credential -Subject "Exchange Daily Tracking Log" -Attachments $File
#Remove CSV files
Remove-Item $File