Outgoing mail stays in the Exchange Server mail queue and is not delivered. What's going…
How to export mailboxes less than total size
We want to get mailboxes in Exchange Server that are in total size less than the amount given. This is an excellent way before we migrate the mailboxes to another database. In this article, you will learn how to export mailboxes less than total size.
Table of contents
Export mailboxes less than total size PowerShell script
The Export-MailboxesTotalSize.ps1 will get all mailboxes that are less than the total sum size you define in the script. For every mailbox, it gathers the following information:
- DisplayName
- TotalSizeGB
- UserPrincipalName
- PrimarySMTPAddress
- SMTPAliasAddresses
Note: If you define a total size of 100 GB in the script and two mailboxes are 50 GB in size, it will only show these two mailboxes. That’s because summing up both mailboxes is a total of 100 GB.
Prepare Export-MailboxesTotalSize PowerShell script
Create two folders on the Exchange Server (C:) drive:
- Temp
- Scripts
Download and place Export-MailboxesTotalSize.ps1 PowerShell script in C:\scripts folder. The script will export the CSV file to the C:\temp folder.
Ensure the file is unblocked to prevent errors when running the script. Read more in the article Not digitally signed error when running PowerShell script.
Another option is to copy and paste the below code into Notepad. Give it the name Export-MailboxesTotalSize.ps1 and place it in the C:\scripts folder.
<#
.SYNOPSIS
Export-MailboxesTotalSize.ps1
.DESCRIPTION
Export mailboxes that are less than the defined total size.
.LINK
www.alitajran.com/export-mailboxes-total-size-less-than/
.NOTES
Written by: ALI TAJRAN
Website: www.alitajran.com
LinkedIn: linkedin.com/in/alitajran
.CHANGELOG
V1.00, 10/22/2023 - Initial version
#>
# Define the target total size in GB
$targetTotalSizeGB = 100
# Specify the mailbox database name to search within
$mailboxDatabaseName = "DB01"
# Define the export file path
$exportFilePath = "C:\temp\Mailboxes.csv"
# Get a list of mailbox sizes within the specified mailbox database
$mailboxSizes = Get-Mailbox -ResultSize Unlimited -Database $mailboxDatabaseName |
Get-MailboxStatistics |
Select-Object DisplayName, @{Name = "TotalItemSizeGB"; Expression = { [math]::Round($_.TotalItemSize.Value.ToBytes() / 1GB, 2) } } |
Sort-Object TotalItemSizeGB
# Initialize variables
$selectedMailboxes = @()
$currentTotalSizeGB = 0
# Iterate through mailboxes to find a combination that adds up to the target total size
foreach ($mailbox in $mailboxSizes) {
$mailboxSizeGB = $mailbox.TotalItemSizeGB
# Check if adding this mailbox keeps the total size within or equal to the target size
if (($currentTotalSizeGB + $mailboxSizeGB) -le $targetTotalSizeGB) {
$selectedMailbox = Get-Mailbox $mailbox.DisplayName -ResultSize Unlimited
$upn = $selectedMailbox.UserPrincipalName
$smtpPrimary = $selectedMailbox.PrimarySmtpAddress
$smtpAliases = $selectedMailbox.EmailAddresses | Where-Object { $_.PrefixString -eq "smtp" -and $_.SmtpAddress -ne $smtpPrimary } | ForEach-Object { $_.SmtpAddress }
$selectedMailboxes += [PSCustomObject]@{
'DisplayName' = $mailbox.DisplayName
'TotalSizeGB' = $mailbox.TotalItemSizeGB
'UserPrincipalName' = $upn
'PrimarySmtpAddress' = $smtpPrimary
'SMTPAliasAddresses' = $smtpAliases -join ", "
}
$currentTotalSizeGB += $mailboxSizeGB
# Check if the current total size equals the target size or exceeds it
if ($currentTotalSizeGB -ge $targetTotalSizeGB) {
break
}
}
}
# Display the selected mailboxes in an interactive table
$selectedMailboxes | Sort-Object TotalSizeGB -Descending | Out-GridView -Title "Selected Mailboxes"
Write-Host "Total Size: $currentTotalSizeGB GB" -ForegroundColor Cyan
# Export the selected mailboxes to a CSV file
$selectedMailboxes | Sort-Object TotalSizeGB -Descending | Export-Csv -Path $exportFilePath -NoTypeInformation -Encoding UTF8
# Display a message indicating successful export
Write-Host "File exported successfully to $exportFilePath" -ForegroundColor Green
- Line 21: Edit the total size
- Line 24: Edit the database name
- Line 27: Edit the export CSV file path
Run Export-MailboxesTotalSize PowerShell script
Run Exchange Management Shell as administrator. Change the path to the scripts folder. After that, run the Export-MailboxesTotalSize.ps1 script.
PS C:\> cd c:\scripts\
PS C:\scripts> .\Export-MailboxesTotalSize.ps1
The output will show the total size in GB that it did gather.
Total Size: 1.18 GB
Check mailboxes in Out-GridView
An Out-GridView will show columns with the mailboxes and their total size in GB.
Open mailboxes report CSV file
The Export-MailboxesTotalSize.ps1 PowerShell script will export the mailboxes less than the total size to CSV file. Find the file Mailboxes.csv in the path C:\temp.
Open the CSV file with your favorite application. In this example, it’s Microsoft Excel.
The mailboxes less than the total size report look great.
Note: Do you want to export all the mailboxes to CSV file and get all their mailbox size and more information? Read the article Get mailbox size of all users in Exchange with PowerShell.
Conclusion
You learned how to export mailboxes less than total size with Powershell. Get the mailboxes less than total size report with the Export-MailboxesTotalSize.ps1 PowerShell script and have a good look into it.
Did you enjoy this article? You may also like Migrate Exchange mailboxes with CSV file. Don’t forget to follow us and share this article.
GREAT ARTICLE, CAN DO SIZE LIMIT ON USER MAILBOX MIGRATION, HELPED ME A LOT, THANKS ALI TAJRAN!