Report when mailbox reaches a particular size.

ESJ 41 Reputation points
2024-03-01T12:54:10.2133333+00:00

I am trying to write a powershell script to be put in scheduler, so that email an be sent to admin when the recoverable items folder of any mailbox in the organization reaches 90 GB.

Can someone please help ?

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
3,808 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,185 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AH 5 Reputation points
    2024-03-01T13:15:37.3+00:00

    To accomplish this task, you can use PowerShell along with Exchange Online cmdlets to monitor the size of the recoverable items folder in mailboxes. Here's a basic script outline to get you started:

    powershellCopy code
    # Connect to Exchange Online
    $UserCredential = Get-Credential
    Connect-ExchangeOnline -Credential $UserCredential
    
    # Get a list of all mailboxes in the organization
    $mailboxes = Get-Mailbox -ResultSize Unlimited
    
    # Iterate through each mailbox
    foreach ($mailbox in $mailboxes) {
        $mailboxStats = Get-MailboxStatistics -Identity $mailbox.UserPrincipalName
        
        # Check if recoverable items folder size exceeds 90 GB
        if ($mailboxStats.RecoverableItemsQuota -gt 0 -and $mailboxStats.RecoverableItemsWarningQuota -gt 0) {
            $recoverableItemsSize = $mailboxStats.TotalRecoverableItemsSize / 1GB
            $recoverableItemsWarningQuota = $mailboxStats.RecoverableItemsWarningQuota / 1GB
    
            if ($recoverableItemsSize -ge $recoverableItemsWarningQuota) {
                # Send an email notification
                $emailBody = "The recoverable items folder of mailbox $($mailbox.UserPrincipalName) has reached $($recoverableItemsSize) GB, which exceeds the warning quota."
                
                Send-MailMessage -To "admin@example.com" -From "sender@example.com" -Subject "Recoverable Items Folder Size Warning" -Body $emailBody -SmtpServer "smtp.example.com"
            }
        }
    }
    
    # Disconnect from Exchange Online
    Disconnect-ExchangeOnline -Confirm:$false
    

    Before running the script, make sure you have the necessary permissions to access Exchange Online PowerShell cmdlets, and replace the placeholders such as admin@example.com, sender@example.com, and smtp.example.com with your actual email addresses and SMTP server.

    Also, ensure that you schedule this script to run periodically using Task Scheduler or any other scheduler tool to monitor the recoverable items folder sizes continuously.