We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script Repository

Save mailbox size to Active Directory attribute

November 23, 2023 Views: 3435

The script saves mailbox size to an Active Directory attribute. This can be used to quickly assess mailbox size of multiple mailboxes at once, for example, if you want to include the mailbox size in reports.

To use the script in your environment, create a scheduled task that runs it on a periodical basis. The task must be configured for the User object type.


  • $attributeName - Specifies the name of the attribute to hold the mailbox size.
Edit Remove
$attributeName = "adm-CustomAttributeText1" # TODO: modify me

# Get Exchange properties
$mailboxParams = $Context.TargetObject.GetMailParameters()
$sizeInBytes = $mailboxParams.UsageInfo.Size.TotalSize

if ($sizeInBytes -eq $NULL)

# Get mailbox size
switch -Regex ([Math]::Truncate([Math]::Log($sizeInBytes, 1024))) {
    '^1' { $sizeDisp = "{0:N2} KB" -f ($sizeInBytes / 1KB) }
    '^2' { $sizeDisp = "{0:N2} MB" -f ($sizeInBytes / 1MB) }
    '^3' { $sizeDisp = "{0:N2} GB" -f ($sizeInBytes / 1GB) }
    '^4' { $sizeDisp = "{0:N2} TB" -f ($sizeInBytes / 1TB) }
    Default { $sizeDisp = "$sizeInBytes Bytes" }

# Update attribute
$Context.TargetObject.Put($attributeName, $sizeDisp)
Comments 0
Leave a comment

Got questions?

Support Questions & Answers