PS Script : Mailbox size Eceede 90 GB

Austin Sundar 20 Reputation points
2024-05-23T08:55:16.2+00:00

I am writing a script to retrieve Exchange Online mailboxes that exceed the 90 GB size limit. The script below throws some errors. Could anyone help?

Capture


# Get all user mailboxes
$mailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited

# Loop through each mailbox and get the mailbox statistics
foreach ($mailbox in $mailboxes) {
    $mailboxStats = Get-MailboxStatistics -Identity $mailbox.UserPrincipalName
    
    # Calculate total mailbox size in GB
    $totalSizeGB = [math]::Round($mailboxStats.TotalItemSize.Value.ToBytes() / 1GB, 2)

    # Check if the mailbox size exceeds 90 GB
    if ($totalSizeGB -gt 90) {
        $recoverableItemsSizeGB = [math]::Round($mailboxStats.RecoverableItemsSize.Value.ToBytes() / 1GB, 2)
        $deletedItemsSizeGB = [math]::Round($mailboxStats.DeletedItemSize.Value.ToBytes() / 1GB, 2)

        # Add the mailbox details to the list
        $mailboxDetails += [PSCustomObject]@{
            UserPrincipalName = $mailbox.UserPrincipalName
            DisplayName       = $mailbox.DisplayName
            TotalSizeGB       = $totalSizeGB
            RecoverableItemsSizeGB = $recoverableItemsSizeGB
            DeletedItemsSizeGB = $deletedItemsSizeGB
        }
    }
}

# Export the mailbox details to a CSV file
$mailboxDetails | Export-Csv -Path $outputCsv -NoTypeInformation


Microsoft Exchange Online
Microsoft Exchange
Microsoft Exchange
Microsoft messaging and collaboration software.
435 questions
{count} votes

Accepted answer
  1. Vasil Michev 98,946 Reputation points MVP
    2024-05-23T16:25:46.98+00:00

    Exchange Online cmdlets return sizes as string value, you cannot use methods such as .ToBytes() on those. Try something like this instead:

    [double]((($mailboxStats.TotalItemSize.Value.ToString()).split(" "))[2]).trimstart("(")/1GB
    
    0 comments No comments

0 additional answers

Sort by: Most helpful