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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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?
# 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
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