Share via


Using PowerShell to Get Mailbox Database Size and Available New Mailbox Space

Introduction

The Exchange Server does not work with flat files, and most of its activity is saved and stored in a database. Like all databases, be it SQL or any other, Exchange databases also need some maintenance. With constant additions, changes and deletions taking place, the database needs to be maintained and cleared off with unnecessary storage so as to have a healthy database.

You don’t want your Exchange Server database storage getting filled to the brim and ending up with no user being able to save or change any information in the mailbox. Also, not forgetting the possibility to end up with a corrupted exchange database. Therefore, prevention is always the best route. You never want to end up with a database server going down in the middle of the night or on a weekend. So, let’s discuss how to use PowerShell to get more information on the database, by checking the size and unwanted extra space it takes.

What is Unwanted Space?

Unwanted space, also called “white space”, is the space accumulated when, for example, a user transfers a file or mailbox of 5 GB on the database, the database is expanded with 5 GB. After some time, several mails or mailboxes are deleted from the database. Although in theory the size of the mailbox database should go down after deletion, but this space is not purged, and the size of database remains as it was before deleting the data. Another example is: When the users clear their deleted items, the database will not automatically shrink. This is a mechanism from the Exchange Server. The database do not shrink and is extended on many occasions, thus increasing the impact on the storage and ultimately on the performance of the Exchange Server. It is similar to the concept of how Microsoft SQL databases work.

How to Use PowerShell to Get Mailbox Database Size?

Let’s start by opening the Exchange Management Shell on the Exchange Server and use the Get-MailboxDatabase PowerShell cmdlet.

If you simply run the cmdlet, it will not show the information you need. But you need to filter it to get the desired information related to the databases.

This is ideal when you have a setup with multiple databases in its environment as it will give information of all the databases. You can use the following command, but it will give all the information, which may not be relevant to you.

Get-MailboxDatabase | FL

To get specific information, you need to use the pipe and ‘Select’ parameter as given in the command below.

Get-MailboxDatabase | Select Name, DatabaseSize, AvailableNewMailboxSpace

As you can see above, the two returned parameters are empty. This is because the status parameter is missing in the command which can retrieve the extra details that you require. You must add -Status to the command as the default syntax does not return the data.

Get-MailboxDatabase -status| Select Name, DatabaseSize, AvailableNewMailboxSpace

Here, in the result, you can see all the details related to all your databases, with the database sizes and the available free space in them.

Now, for reporting purposes, you wouldn’t want the results in bytes and megabytes. To facilitate the divisions and copying the data into an Excel or CSV, you would want to look into getting the export of this more linear so that you can import it into your report system. You can do this by updating the command, as below:

Get-MailboxDatabase -Status | sort name | select name,@{Name='DB Size';Expression={$_.DatabaseSize.ToGb()}},@{Name='Free Space';Expression={$_.AvailableNewMailboxSpace.ToGb()}}

If you want them in megabytes, you can change the ToGb to ToMb as given below:

Get-MailboxDatabase -Status | sort name | select name,@{Name='DB Size';Expression={$_.DatabaseSize.ToMb()}},@{Name='Free Space';Expression={$_.AvailableNewMailboxSpace.ToMb()}}

Conclusion

In the beginning of this article, we talked on the prevention of a disaster, where the storage of mailbox databases is consumed by free space and unnecessary items, resulting in filling up your hard drives. When this happens, you will end up with users unable to send or receive emails, with the possibility of corruption in Exchange databases. As said, prevention and regular maintenance is the cure for 80 percent of your issues. But when disaster strikes, you would need to have the right tools in hand.