Export Exchange 2019 primary mailbox and archive mailbox total sizes

mark terry 165 Reputation points
2025-05-10T00:03:58.9633333+00:00

Hi Folks,

I have the script below which exports the TotalItemSize of primary and archive mailboxes for users taken from an input file. As you can see from the example output, the TotalItemSize shows the value in MB and bytes. What I would like to do is convert the TotalItemSize values to be in MB only i.e. do not include the bytes section.

"Alias","PrimaryMailboxSize","ArchiveMailboxSize"

"smith1","2.945 MB (3,087,594 bytes)","673 KB (689,202 bytes)"

My existing script is below. Note, I am not particularly tied to this script, so if you have something else which works, that would be fine too!

Any help would be appreciated!

Thanks,

Mark

# Import the CSV file
$mailboxes = Import-Csv "E:\Scripts\Mailbox-Migration\Master-User-List.csv"

# Create an array to store the results
$results = @()

# Loop through each email address
foreach ($mailbox in $mailboxes) {
    $alias = $mailbox.alias

    # Get primary mailbox size
    $primaryMailbox = Get-MailboxStatistics -Identity $alias
    $primarySize = $primaryMailbox.TotalItemSize

    # Get archive mailbox size (if enabled)
    $archiveMailbox = Get-MailboxStatistics -Identity $alias -Archive
    $archiveSize = $archiveMailbox.TotalItemSize

    # Create a custom object to store the results
    $result = [PSCustomObject]@{
        Alias = $alias
        PrimaryMailboxSize = $primarySize
        ArchiveMailboxSize = $archiveSize
    }

    # Add the result to the array
    $results += $result
}

# Export the results to a CSV file
$results | Export-Csv -Path "E:\Scripts\Mailbox-Migration\Get-Primary-and-Archive-Mailbox-Sizes\mailbox_sizes.csv" -NoTypeInformation

write-host
Write-host -ForegroundColor Green "Mailbox sizes have been exported to E:\Scripts\Mailbox-Migration\Get-Primary-and-Archive-Mailbox-Sizes\mailbox_sizes.csv"

Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,885 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Vasil Michev 117.9K Reputation points MVP Moderator
    2025-05-10T15:34:30.9966667+00:00

    You can address this by simple string manipulation, for example:

    PrimaryMailboxSize = $primarySize.Split()[0..1] -join " "
    

    Basically, we're splitting the string to space-delimited substrings, then taking the first two of these, which should give you the size in "2.945 MB" format, and joining them back to a single string.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.