It's essential to configure the TCP KeepAliveTime in Exchange Server. By default, it's not set,…
Get mailbox size of all users in Exchange with PowerShell
We want to get the mailbox size of all users in Exchange with PowerShell. But why do we need to list the mailbox size for all users? For example, you want to migrate the users to Exchange Server/Exchange Online. Before doing that, find all the users’ mailbox sizes in Exchange Server/Exchange Online. The script that we will use to display or export mailbox statistics in this article will gather 22 types of data per mailbox.
Table of contents
Information get mailbox size of all users PowerShell script
The MailboxSizeReport.ps1 PowerShell script works for:
- Exchange on-premises
- Exchange Online
You must connect with the proper tools before you run the script:
- Exchange on-premises: Run Exchange Management Shell as administrator.
- Exchange Online (Microsoft 365/Office 365): Run PowerShell as administrator and connect to Exchange Online PowerShell.
The MailboxSizeReport.ps1 PowerShell script got 7 options:
- Display in Exchange Management Shell
- Export to CSV File
- Export to CSV File (Specific to Database)
- Enter the Mailbox Name with Wild Card (Export)
- Enter the Mailbox Name with Wild Card (Display)
- Export to CSV File (OFFICE 365)
- Enter the Mailbox Name with Wild Card (Export) (OFFICE 365)
Note: Use options 1-5 for Exchange On-Premises. Use options 6-7 for Exchange Online (Microsoft 365/Office 365).
The script will run and find all users with a mailbox. It will gather the following information per mailbox in the CSV file:
- Display Name
- Alias
- SamAccountName
- RecipientType
- Recipient OU
- Primary SMTP address
- Email Addresses
- Database
- ServerName
- TotalItemSize
- ItemCount
- DeletedItemCount
- TotalDeletedItemSize
- ProhibitSendReceiveQuota-In-MB
- UseDatabaseQuotaDefaults
- LastLogonTime
- ArchiveName
- ArchiveStatus
- ArchiveState
- ArchiveQuota
- ArchiveTotalItemSize
- ArchiveTotalItemCount
Get mailbox size of all users with PowerShell script
Download and place MailboxSizeReport.ps1 PowerShell script in the C:\scripts folder. If you don’t have a scripts folder, create one.
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.
Run Exchange Management Shell or Connect to Exchange Online PowerShell. This depends on which option you like to choose in the script.
Run the following commands to change the directory path and start the script. It will display the Mailbox Size Report script.
[PS] C:\>cd c:\scripts
[PS] C:\scripts>.\MailboxSizeReport.ps1
Mailbox Size Report
----------------------------
1.Display in Exchange Management Shell
2.Export to CSV File
3.Export to CSV File (Specific to Database)
4.Enter the Mailbox Name with Wild Card (Export)
5.Enter the Mailbox Name with Wild Card (Display)
6.Export to CSV File (OFFICE 365)
7.Enter the Mailbox Name with Wild Card (Export) (OFFICE 365)
Choose The Task:
The script got 7 options. The one that we are looking for is option 2. Export to CSV File.
Insert 2 and press Enter.
[PS] C:\scripts>.\MailboxSizeReport.ps1
Mailbox Size Report
----------------------------
1.Display in Exchange Management Shell
2.Export to CSV File
3.Export to CSV File (Specific to Database)
4.Enter the Mailbox Name with Wild Card (Export)
5.Enter the Mailbox Name with Wild Card (Display)
6.Export to CSV File (OFFICE 365)
7.Enter the Mailbox Name with Wild Card (Export) (OFFICE 365)
Choose The Task: 2
Enter the Path of CSV file (Eg. C:\Report.csv):
The Mailbox Size Report PowerShell script will ask for a path. The export of the CSV file will get exported to that path.
Insert the following path: C:\scripts\Report.csv and press Enter.
[PS] C:\scripts>.\MailboxSizeReport.ps1
Mailbox Size Report
----------------------------
1.Display in Exchange Management Shell
2.Export to CSV File
3.Export to CSV File (Specific to Database)
4.Enter the Mailbox Name with Wild Card (Export)
5.Enter the Mailbox Name with Wild Card (Display)
6.Export to CSV File (OFFICE 365)
7.Enter the Mailbox Name with Wild Card (Export) (OFFICE 365)
Choose The Task: 2
Enter the Path of CSV file (Eg. C:\Report.csv): C:\scripts\Report.csv
The Mailbox Size Report PowerShell script starts scanning the mailboxes in the organization. This can take some time. When done, a list is created and exported in a CSV file.
You can exit the script.
Result mailbox size report of all users in Exchange
Let’s see if the script made the export successful by going to the following path: C:\scripts\. You should see the Report.csv file.
Open mailbox size report with Microsoft Excel
To import all the Mailbox Size Report data from CSV file into Microsoft Excel, follow these steps:
1. Start Microsoft Excel and open a new blank workbook.
2. Select Data > From Text/CSV.
3. Select the CSV file and click on Import.
4. Check that all the columns are visible.
5. Verify that TotalItemSize is available and click on Load.
6. The Report.csv is loaded in Microsoft Excel.
7. Sort the column TotalItemSize from largest to smallest size.
If you don’t need all the information, you can remove those columns. You can sort the file by Display Name, TotalItemSize, or any other type. Tweak the information to your needs.
Everything looks great!
Keep reading: Export Office 365 mailbox permissions to CSV »
Conclusion
You learned how to get the mailbox size of all users in Exchange/Exchange Online with PowerShell. Use the MailboxSizeReport.ps1 PowerShell script to list the mailbox size for all users in the organization. It’s a great script, and you should use it to get the mailbox size and extra information.
Did you enjoy this article? You may also like Export AD users to CSV with PowerShell. Don’t forget to follow us and share this article.
Can you also report on the recoverable items size too perhaps?
When I ran this script, I received below error
Method invocation failed because [Deserialized.Microsoft.Exchange.Data.ByteQuantifiedSize] does not contain a method named ‘ToMB’.
At C:\Users\XXXXXX\Reports\Mailboxsizereports.ps1:124 char:17
+ … $userObj | Add-Member NoteProperty -Name “TotalDeletedIte …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (ToMB:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Run the script from Exchange Management Shell on the Exchange Server.
Hi Ali,
Thanks so much for this – it’s a great resource.
In the CSV output, the TotalItemSize is displayed as GB, MB and Bytes, depending on the total size. This makes sorting and filtering difficult. Is there any way to get it to output only in bytes or MB?
The following expression has worked to achieve similar, but I am not sure it could be implemented in your script
Expression={[math]::Round(($_.FolderSize.ToString().Split(“(“)[1].Split(” “)[0].Replace(“,”,””)/1MB),0)}}, ItemCount
I am looking for such more information like department , title enable or not, last login etc. can we add these kind of information in it ?
Hello Mr. Ali Tajran,
I’m having the same problem as Ab.
On one MSExch19 server I get complete CSV with Task2 as expected, on another I get missing TotalItemSize,ItemCount,DeletedItemCount and TotalDeletedItemSize columns .
I tried Task1 as test and it shows correctly.
I believe the problem is somewhere with variable $Stats on line 85, because I think on line 120 the variable resolves to null.
Unfortunately when I run the commands in EMS $Stats populates okay, so I am stumped at how to solve this.
Any idea why’s this happening or how to solve it, or what I could try to find out?
Anyway, as this is the first time I am commenting – thank you very much for clear and educational website as this!
Regards,
Zdražilová
Open the CSV file with Notepad and verify.
I tested both tasks 1 and 2 right now on a couple of Exchange Server 2019 servers. Everything works fine and the report shows all columns and values.
Hi Ali,
Thank you for your effort making this article.
if you may help, can we make this automate via task scheduler and send the csv file via email?
thank you
Hi Ali,
Great website, wonderful resources! I’m using the script in Exchange 2016, I have 121 users. The total itemsize is blank in the CSV. Any ideas? I’m on the latest CU version with all updates.
Thanks in advance,
Ab
Only the TotalItemSize? Or more columns?
Open the CSV file with Notepad and check if the values appear. I tried it right now, and everything works. Most likely, this has to do with the CSV file import.
If you go through the exact steps, you will not have any issues.
Good day,
The script works well, thank you. Was wondering if it can be altered to specify a date range for the mailbox and archive mailbox size
Hello ALI,
Your blog is excellent which saves our time on activities.
In this script if I want to find the AccountDisabled Status, please can you help me to add as I need to find the AccountDisabled for all the mailboxes as I am working on mailbox consolidation.
Regards
Anand Sunka
Thank you for the script.
Hello Ali,
Thank you for this script.
Hi,
How can script give the results in KB and not in MB ? Where can I put the “=convertto KB” ?
In rest this script work very well.
Hi Ali, Thank you for the script. it’s working fine.
May I know the Total count is in what type? KB or GB?
Assuming I have this results,
240
9731
26117
Thank You
Hi Norafify,
The total count shows in MB, which means that the example of yours is as:
240 MB
9731 MB
26117 MB
Hi, i only get a lot of errors, when i try to run the script.
You did get these errors because the download didn’t save as it should. Please try again and ensure that you do save the PowerShell script correctly.
A good way is to open the PowerShell script with PowerShell ISE or Notepad and verify it looks good.
Hi,
Could you help me with a script to export archive size too?
Thank you
Hi Mo,
The script already has that feature. The export will show the archive size in the column ArchiveTotalItemSize.
I got this error message, how do I fix it?
Choose The Task: 1
Get-mailbox : The term ‘Get-mailbox’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At line:117 char:15
+ $AllMailbox = Get-mailbox -resultsize unlimited
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-mailbox:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
You get this error because you are running the script from PowerShell/PowerShell ISE.
For options 1-5, run Exchange Management Shell and run the script.
Tnx Dear Ali
I get this messege in Exchange online:
Method invocation failed because [Deserialized.Microsoft.Exchange.Data.ByteQuantifiedSize] does not contain a method named ‘ToMB’. At C:\Scripts\Mailboxsize1.ps1:192 char:1 + $userObj | Add-Member NoteProperty -Name “TotalItemSize” -Value $Stat … + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (ToMB:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
You get this message because you are running option “2.Export to CSV File”. Use that option if you want a report for the Exchange on-premises mailboxes.
In your case, you want a report for the Exchange Online mailboxes. You need to select option “6.Export to CSV File (OFFICE 365)”.
These are the steps:
1. Connect to Exchange Online PowerShell
2. Run MailboxSizeReport.ps1 PowerShell script
3. Select option “6.Export to CSV File (OFFICE 365)”
4. Insert export path
thank you a lot.
can we schedule it in task scheduler with attributes 2 of exporting and constant shared location ?
hi
is possible add if account in active directory is disable or no?
thz
Hello, Ali.
Thank you for this script.
I’d like to understand why did you use the BigFunnelMessageCount instead of the ItemCount when counting archive messages?