Thursday, June 10, 2010

Exchange 2007 Script - 30 day old Deleted Items with email report

My current Exchange environment is a decentralized one where I maintain one configuration and others maintain their own.  We do not have mailbox policies configured for the entire organization.  So I wanted to remove any items in my users Deleted Items folders that are more than 30 days old and then be able to see how many items were removed and how much space this freed up.  This use to be easy in Exchange 2003 with Mailbox Maintenance Policies.

After several days of learning Powershell scripting and trying so many different syntaxes, I finally have a working script.  I can't say that I know much about Powershell but I definitely know a lot more than I did a week ago.  This script will need to be modified to fit your environment but I added documentation on where to do that.  You can copy the text below into your own text file and save it with the .ps1 extension.  If you copy be sure to watch for line wrapping.  Once you have made the few modifications for your environment you can set this to run on a schedule with Windows Task Scheduler.




#This Powershell script will go through every mailbox on your Exchange 2007 mailbox server and will delete
#all items in the Deleted Items folder that are more than 30 days old and then emails a report to you.
#You can set this with Windows Task Manager to run on a set schedule.
#Written by Brad Goodman

#Sets the $date variable to 30 days before today.
$date = Get-Date
$date = $date.AddDays(-30)
$date = $date.ToShortDateString()

#Sets the output file name.
$filename = "DeletedItems{0:MMddyyyy}" -f (Get-Date)

#Defines the output table.
$mbxcomb = "" | Select "Display Name", "Items Deleted", "Space Saved(KB)"

#Begins a transcipt and saves it in the Documents folder of the user running the script.
Start-transcript -Path $home\documents\$filename.txt

#Calls the mailboxes.  You can set a -database or -server to be more specific after the Get-Mailbox.
#I suggest that you set the -server to your mailbox server if you don't manage all in your Exchange org.
Get-Mailbox -server "your mailbox server" | foreach {

$mbx = $_.DisplayName
$strmbxUser = $NULL

#Determine the initial size of the mailbox in KB.
$intSizeBefore = (Get-MailboxStatistics $mbx).TotalItemSize.Value.ToKB()

#Removes any items in the Deleted Items folder that are older than 30 days.
$strMbxUser = Export-Mailbox $mbx –includefolders “\Deleted Items” –StartDate “1/1/1995” –EndDate $date –DeleteContent -Confirm:$False

#Save the number of deleted items to the variable $intTotalDeleted.
$intTotalDeleted = $strmbxUser.StandardMessagesDeleted

#Gets the new size of the mailbox after deleting items and then finds the amount of space saved in KB.
$intSizeAfter = (Get-MailboxStatistics $mbx).TotalItemSize.Value.ToKB()
$intSpaceSaved = $intSizeBefore - $intSizeAfter

#Stores the specifics about each mailbox in the table.
$mbxcomb."Display Name" = $mbx
$mbxcomb."Items Deleted" = $intTotalDeleted
$mbxcomb."Space Saved(KB)" = $intSpaceSaved

#Creates totals of mailboxes processed, items deleted, and space saved through each iteration of the
#get-mailbox.
$intmbxTotal = $intmbxTotal + 1
$intmbxDeleted = $intmbxDeleted + $intTotalDeleted
$intmbxSaved = $intmbxSaved + $intSpaceSaved

#Outputs the table to the screen so that the transcript can capture it.
Write-output $mbxcomb

}  #End of the Foreach loop.

#Outputs the totals for all mailboxes to the screen so the transcript can capture it.
Write-Host ("`n=======================================`n")
Write-Host ("Mailboxes Processed = $intmbxTotal")
Write-Host ("Total Items Deleted = $intmbxDeleted")
Write-Host ("Total Space Saved(KB) = $intmbxSaved")
Write-Host ("`n=======================================`n")

Stop-Transcript
$transcript = (Get-Content $home\documents\$filename.txt | Out-String)

#Emails transcript file.  Replace smtphost, domain, user, password, sender, recipient with your own.
$SmtpClient = new-object system.net.mail.smtpClient
$smtpclient.Host = 'smtphost'
$Credentials = new-object System.Net.networkCredential
$Credentials.domain = "domain"
$Credentials.UserName = "user"
$Credentials.Password = "password"
$SMTPClient.Credentials = $Credentials
$smtpclient.Send('sender', 'recipient', "Exchange Deleted Items Report - {0:MM/dd/yyyy}" -f (Get-Date), $transcript)

4 comments:

  1. Do you have a way of doing this in Exchange 2010 sp1? The Export-Mailbox command no longer works in Exchange 2010 after SP1. Thanks.

    ReplyDelete
  2. Nice script. Thank You very much!

    One thing, make sure the user that is running the scrip has full access permissions. Otherwise you will receive MAPI Errors. Thanks!

    ReplyDelete