Home > Exchange 2007, Exchange 2010, Scripting > Exchange 2010 PowerShell:List Junk E-mail, Delete Items Folder

Exchange 2010 PowerShell:List Junk E-mail, Delete Items Folder

How much overhead is there in Exchange 2010 with regard to mailbox content and storage consumption in the Junk Email and Deleted Items folders?  This topic often comes up as expensive SANs and Storage arrays fill up, especially with thin-provisioned storage arrays, such as Equallogic designs for Exchange.

There are 2 main topics that I wanted to check on each Exchange 2007 Mailbox Server:

  1. How much data is stored in the Deleted Items Folder
  2. How much data is stored in the Junk E-Mail Folder

My experience has shown that 10-15% of your overall Exchange data is located in one of these two folders if your organization does not institute some type managed folder or retention policy in your Exchange organization.

I have written a script to pull the Mailbox size, Deleted Items, and Junk Email Size out of Exchange.  For large Exchange organizations, this script could take some time to run.  The script dumps all of the data to an Excel spreadsheet that is then emailed to the email administrator.   At the end of the script you will have to define the Recipient, Sender and SMTP server.

$date = get-date
#Author: Ed McKinzie

$erroractionpreference = "SilentlyContinue"
$warningactionpreference = "SilentlyContinue"
#View the Entire Forest
Set-ADServerSettings -ViewEntireForest:$True
[Array] $MailboxVariablesCollection = @() 

# Hit all Mailboxes
(Get-Mailbox -ResultSize Unlimited) | ForEach { 

$erroractionpreference = "SilentlyContinue"
$warningactionpreference = "SilentlyContinue"
  # Get the DisplayName, LastLogonTime, DatabaseName and TotalItemSize statistics 
  $MailboxStats = Get-MailboxStatistics $_.identity | Select DisplayName, LastLogonTime, DatabaseName, TotalItemSize 

  # Enumerate Sent, Junk-Email and Deleted Items 
  $mbSentStats = Get-MailboxFolderStatistics $_.identity | Where {$_.FolderPath -eq "/Sent Items"} | Select ItemsInFolderAndSubfolders, @{name="SentItemsSize";expression={$_.FolderAndSubfolderSize.ToMB()}} 
  $mbDeletedStats = Get-MailboxFolderStatistics $_.identity | Where {$_.FolderPath -eq "/Deleted Items"} | Select ItemsInFolderAndSubfolders, @{name="DeletedItemsSize";expression={$_.FolderAndSubfolderSize.ToMB()}} 
  #$mbJunkEmailStats = Get-MailboxFolderStatistics $_.identity –FolderScope JunkEmail | Select ItemsInFolderAndSubfolders,@{name="JunkEmailItemCount";expression={$_.FolderAndSubfolderSize.ToMB()}} 
  $mbJunkEmailStats = Get-MailboxFolderStatistics $_.identity | Where {$_.FolderPath -eq "/Junk E-mail"} | Select ItemsInFolderAndSubfolders,@{name="JunkEmailItemCount";expression={$_.FolderAndSubfolderSize.ToMB()}} 
  $mbManagedFolderStats = Get-MailboxFolderStatistics $_.identity | Where {$_.FolderPath -eq "/Managed Folders"} | Select ItemsInFolderAndSubfolders,@{name="ManagedFolderItemCount";expression={$_.FolderAndSubfolderSize.ToMB()}}

  $MailboxVariables = "" | Select "Display Name", "Last Logon Time", Database, "Mailbox Size (MB)", "Total Sent Items", "Sent Items Size (MB)", "Total Deleted Items", "Deleted Items Size (MB)", "JunkEmailItemCount", "TotalJunkEmailSizeMB","ManagedFolderItemCount","ManagedFolderEmailSizeMB","E-mail Address" 

  $MailboxVariables."Display Name" = $MailboxStats.DisplayName 
  $MailboxVariables."Last Logon Time" = $MailboxStats.LastLogonTime 
  $MailboxVariables.Database = $MailboxStats.DatabaseName 
  $MailboxVariables."Mailbox Size (MB)" = "{0:n2}" -f ($MailboxStats.TotalItemSize.Value.ToMB()) 
  $MailboxVariables."Total Sent Items" = "{0:n1}" -f ($mbSentStats.ItemsInFolderAndSubfolders)
  $MailboxVariables."Sent Items Size (MB)" = "{0:n2}" -f ($mbSentStats.SentItemsSize) 
  $MailboxVariables."Total Deleted Items" = "{0:n1}" -f ($mbDeletedStats.ItemsInFolderAndSubfolders)
  $MailboxVariables."Deleted Items Size (MB)" = "{0:n2}" -f ($mbDeletedStats.DeletedItemsSize)

  $MailboxVariables."TotalJunkEmailSizeMB" = "{0:n2}" -f ($mbJunkEmailStats.JunkEmailItemCount)
  $MailboxVariables."JunkEmailItemCount" = "{0:n1}" -f ($mbJunkEmailStats.ItemsInFolderAndSubfolders)

  $MailboxVariables."ManagedFolderEmailSizeMB" = "{0:n2}" -f ($mbManagedFolderStats.ManagedFolderItemCount)
  $MailboxVariables."ManagedFolderItemCount" = "{0:n1}" -f ($mbManagedFolderStats.ItemsInFolderAndSubfolders)

  $MailboxVariables."E-mail Address" = $_.PrimarySmtpAddress 

  $MailboxVariablesCollection += $MailboxVariables 

$MailboxVariablesCollection | Export-Csv C:Scripts"MailboxStats_All_Mailboxes_$(Get-Date -f 'yyyyMMdd').csv" -NoType

Send-MailMessage -To "oneyw@vlab.com" -From "Oneyw@vlab.com" `
        -Subject "Exchange 2010 Mailbox Junk Email and Deleted Items Folders - $Date" -SmtpServer "ex-cas1.vlab.com" -BodyAsHtml -Attachments C:Scripts"MailboxStats_All_Mailboxes_$(Get-Date -f 'yyyyMMdd').csv"

Invoke-Expression  C:Scripts"MailboxStats_All_Mailboxes_$(Get-Date -f 'yyyyMMdd').csv"

Out-Put is dumped to Excel:


Remedial Actions on cleaning up this data within Exchange:

  • We can create Exchange 2007 Managed Folder Policies or Exchange 2010 Retention Policy Tags, meaning we auto-purge items in specific folders.
  • We can adjust how long the Deleted Items or Dumpster is preserved
  • We can adjust the DIRT (Deleted Item Retention Time) and DMRT (Deleted Mailbox Retention Time)
  • We can adjust quotas and nag limits on usersdatabases

Update: I just recently added a “Managed Folder” query as well, which is defined as part of your Managed Folder or Item Retention Policy.


Best of Luck!
Ed McKinzie
  1. MAX
    June 6, 2012 at 1:14 AM

    Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.
    + CategoryInfo : OperationStopped: (Microsoft.Power…tHelperRunspace:ExecutionCmdletHelperRunspace) [],
    + FullyQualifiedErrorId : RemotePipelineExecutionFailed

    • MAX
      June 6, 2012 at 1:15 AM

      Need help as its giving error.

  2. Yagnesh Ratnagrahi
    June 6, 2012 at 1:58 AM

    Hello Max,

    This error come in exchange 2010 to resolve it add () in the line 12 with as follow:

    (Get-Mailbox -ResultSize Unlimited) | ForEach {

  3. June 7, 2012 at 2:56 PM

    Thanks, I have updated the script. My script editor was more forgiving on the pipeline errors and ran fine.


    Ed M.

  4. Larry
    September 20, 2012 at 1:15 AM

    This looks like a very well written script and appears to be produce the exact information I’ve been looking for.

    A couple of quick questions, I copied and edited the script you provided, do I save that as a .VBS? If not, what is the correct format? Secondly, my assumption is to then run it inside of EMS correct?

    • Larry
      September 20, 2012 at 1:59 AM

      n/m I got it. I pasted it into a text editor to customize the email functionality, and then pasted THAT into EMS. Worked like a charm!

      Thanks again,

    • October 3, 2012 at 11:37 AM

      Any powershell script will need to be saved as a .PS1 file and launched using Exchange PowerShell. You can call the script by the .\ operator.

  5. Pierre Mitham
    September 27, 2012 at 12:32 PM

    Ed is there anyway to add a counter to this so that you can see some progress? I have 2500 mailboxes and it a bit unnerving to see nothing happening for so long

    • October 3, 2012 at 11:46 AM

      Yes, there is.

      After the loop, add a simple write-host command. Such as:
      (Get-Mailbox -ResultSize Unlimited) | ForEach {
      Write-Host $_.identity

      That should do the trick to at least show where and what mailbox it is processing. There are more in depth counters and progress bar options, but they might be overkill for this process.

      Best of Luck,

      Ed McKinzie

  1. December 2, 2014 at 12:34 AM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: