Retrieve Mailbox Folder and Subfolder Size in Office 365 Exchange Online

Introduction

This script can retrieve mailbox folder and subfolder size in Office 365 Exchange Online.

Scenario

Most of you manage your mailbox by using folders. These folders are named with year or other name conventions. When the mailbox is almost full, you will try to archive or delete old email messages. But you found that it’s inconvenient to check the size of each folder in Outlook Web App (OWA). So you are trying to find a workaround to mitigate this problem.

Although, Office 365 administrators can use Get-MailboxFolderStatistics to help helpdesks to check the folder size. It's better to provide a script to reduce the support calls to these administrators in one company.

Prerequisites

This script requires Exchange Web Service Managed API 2.0. Please download and install the package from Microsoft Download Center. This script cannot work correctly without this package.

Script

This script contains the following advanced functions:

You can use this script in the following way.

  1. Open Windows PowerShell.
  2. Run Import-Module cmdlet to import this module file.
    Import-Module filepath\scriptname.psm1

Here are some code snippets for your references.  

PowerShell
Edit|Remove
#Try to recursively search foldersif ($Recurse) { 
    do 
    { 
        $subFolders = $rootFolder.FindFolders($folderView) 
        foreach ($subFolderin$subFolders.Folders) { 
            $sizeInMb = ($subFolder.ExtendedProperties[1].Value / 1048576).ToString("N2") 
            $result = New-Object System.Management.Automation.PSObject 
            $result|Add-Member-MemberType NoteProperty -Name "DisplayName"-Value $subFolder.DisplayName 
            $result|Add-Member-MemberType NoteProperty -Name "Path"-Value $subFolder.ExtendedProperties[0].Value 
            $result|Add-Member-MemberType NoteProperty -Name "Size(MB)"-Value $sizeInMb$PSCmdlet.WriteObject($result) 
  
            if ($subFolder.ChildFolderCount -gt 0) { 
                $PSCmdlet.MyInvocation.BoundParameters.Remove("FolderName"|Out-Null$PSCmdlet.MyInvocation.BoundParameters.Remove("FolderId"|Out-Null$PSCmdlet.MyInvocation.BoundParameters.Add("FolderId",$subFolder.Id) |Out-Null$boundParams = $PSCmdlet.MyInvocation.BoundParameters 
                Get-OSCEXOMailboxFolderSize @boundParams 
            } 
        } 
    } while ($subFolders.MoreAvailable) 
} 

Examples

Example 1: How to display help about Get-OSCEXOMailboxFolderSize.
 To display help about this function, run this command.
Get-Help Get-OSCEXOMailboxFolderSize -Full

Example 2: How to initiate a connection to Office 365 Exchange Online.
 To initiate a connection to Office 365 Exchange Online, please run this command. You must run this example before any other step.
Connect-OSCEXOWebService -Credential (Get-Credential admin@domain01.onmicrosoft.com)

Note The Connect-OSCEXOWebService function creates a new variable called exService. This variable is in the global scope of the current Windows PowerShell session. This variable is used by other functions in the script.

Example 3: How to return the size of Inbox folder and its sub folders.
 To return the size of Inbox folder and its sub folders, please run this command.
Get-OSCEXOMailboxFolderSize -Recurse

Example 4: How to return the size of "Deleted Items" folder.
 To return the size of "Deleted Items" folder, please run this command. Additionally, don’t forget to enclose the folder name in quotes.
Get-OSCEXOMailboxFolderSize -FolderName "Deleted Items"

Example 5: How to return the total size of Inbox folder and its sub folders.
 To return the total size of Inbox folder and its sub folders, please run this command. You can use Measure-Object to calculate the total size.
Get-OSCEXOMailboxFolderSize -FolderName Inbox -Recurse | Measure-Object -Property "Size(MB)" -Sum

Example 6: How to return the total size of folders and its sub folders that have specific path.
 To return the total size of folders and its sub folders that have specific path, please run this command. You can use Measure-Object to calculate the total size.
Get-OSCEXOMailboxFolderSize -FolderName Inbox -Recurse | ?{$_.Path -match "\\Inbox\\SubFolder\\2012"} | Measure-Object -Property "Size(MB)" -Sum

Note Don’t forget to escape the backslash (\) characters.

Additional Resources

Technical Resources:

Windows PowerShell Advanced Function
http://technet.microsoft.com/en-us/library/dd315326.aspx

Working with search folders by using the EWS Managed API
http://msdn.microsoft.com/en-us/library/exchange/dd633690(v=exchg.80).aspx

Forum Threads:
http://community.office365.com/en-us/forums/148/p/72412/271541.aspx
http://community.office365.com/en-us/forums/146/p/51431/180223.aspx