Master Exchange Database Management with PowerShell

Published:11 February 2020 - 4 min. read

Any system administrator responsible for a Microsoft Exchange environment will, most likely, at some point need to manage Exchange databases. Instead of using the Exchange Admin Center admins can use the Get-MailboxDatabase, New-MailboxRepairRequest, and Move-DatabasePath PowerShell cmdlets to make it happen.

During normal operations, you don’t usually need to worry about EDB files. Though they are an integral piece to the functioning of Exchange, day to day operations often don’t involve the databases directly. As stated above, there are a number of scenarios that can call for more direct intervention.

Provided by the Exchange Management Tools, a PowerShell module for managing Exchange is available. Most likely these tools would be installed on your Exchange Server as available in the following instructions.

Let’s dive into some interesting scenarios you might find yourself in.

Loading Exchange Management PowerShell

Note some of these cmdlets are only available for on-premises Exchange.

The generally recommend approached to using the module is via a PowerShell Remoting session to connect remotely to the Exchange Server and load the Exchange cmdlets as shown below.

PS> Set-ExecutionPolicy RemoteSigned
PS> $UserCredential = Get-Credential
PS> $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<ServerFQDN>/PowerShell/ -Authentication Kerberos -Credential $UserCredential
PS> Import-PSSession $Session -DisableNameChecking

Make sure to remove the connection from your PowerShell session when you are done via the command: Remove-PSSession $Session.

Default Locations of EDB Files

Each version of Exchange puts the EDB files in a slightly different location. You’ll see the default locations below. This can come in handy when interacting with these files directly, though with the available PowerShell commands, you may not have to!

  • Exchange Server 2010
    C:\Program Files\Microsoft\Exchange Server\V14\Mailbox Database\
  • Exchange Server 2013
    C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\Mailbox Database Name\
  • Exchange Server 2016 & 2019
    C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\

Finding Mailboxes with Get-MailboxDatabase

Often you just need to know where a given mailbox database is located or any of the many properties available. You can use the Get-MailboxDatabase cmdlet to find a lot of useful information about an Exchange database as seen below.

Though there are many properties returned, we are just interested in the Name, EdbFilePath, and it’s Mounted status. To retrieve the actual mounted value, use the  Status parameter, otherwise no value will be output.

You can see below an example out that the Get-MailboxDatabase cmdlet produces.

PS> Get-MailboxDatabase -Name MyExchangeDatabase -Status | Select-Object Name, EdbFilePath, Mounted | Format-List

Name        : MyExchangeDatabase
EdbFilePath : C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\Mailbox Database 1000001111.edb
Mounted     : True
--snip--

Dismounting Exchange Databases with Dismount-Database

Now that you have some information on your mailbox database with Get-MailboxDatabase, you might choose to dismount the database. To do that, use the  Dismount-Database cmdlet.

The default directory where the EDB files are located isn’t ideal. You may have a better performing and more manageable location to move them to. Before you can do that though, you must dismount the database as shown below.

PS> Get-MailboxDatabase -Name "MyExchangeDatabase" | Dismount-Database -Confirm:$False
PS> Get-MailboxDatabase -Name "MyExchangeDatabase" -Status | Select-Object Name, Mounted | Format-List

Name    : MyExchangeDatabase
Mounted : False
--snip--

Moving Mailbox Databases with Move-DatabasePath

Since you have dismounted the database, now move the mailbox database to a new location.  This is done using the appropriately named, Move-DatabasePath cmdlet.

Wait! What if ran Get-Mailboxdatabase and found that you had circular logging turned on? According to the best practices for moving database files, this needs to be turned off. To do this, use the Set-MailboxDatabase cmdlet.

PS> Set-MailboxDatabase -Name MyExchangeDatabase -CircularLoggingEnabled:$False

Depending on our environment there may be some extra steps to take before doing that. Notably you may need to remove existing mailbox database copies. Mailbox database copies are copies of a database that are distributed and replicated among your Exchange servers. Don’t worry, even though we are removing them here, we are going to add them back!

PS> Remove-MailboxDatabaseCopy -Identity MyExchangeDatabase -Confirm:$False

You should now be able to move the database to the new location. Running Move-DatabasePath will actually relocate the database files to a new path, provided the database is dismounted.

PS> Move-DatabasePath -Identity MyExchangeDatabase -EdbFilePath C:\EDB\MyExchangeDatabase.edb

Success! The databases are moved, now add back the database copies and also turn circular logging back on as shown below.

PS> Add-MailboxDatabaseCopy -Identity MyExchangeDatabase
PS> Set-MailboxDatabase -Name MyExchangeDatabase -CircularLoggingEnabled:$True

Fixing Corrupted Mailboxes with New-MailboxRepairRequest

After the database move, perhaps you’d like to ensure there aren’t any corrupted mailboxes on the database. Conveniently there is a command for that called New-MailboxRepairRequest. This command can be run on an individual mailbox or, if run without parameters ,on all mailboxes within a given database as shown below.

PS> New-MailboxRepairRequest -Database MyExchangeDatabase

Important! After you begin the repair request, you must dismount the database to stop the process. Additionally, only the mailbox being repaired is inaccessible, all others remain accessible during the database repair.

Gluing PowerShell and ESEUTIL Together

One of the powerful capabilities with PowerShell is the ability to glue many different tools together. A go-to utility for many Exchange Administrators is eseutil.exe.

Perhaps you’d like to defragment an EDB file. Defragmentation can be done via eseutil. An example of this is the following script that illustrates combining PowerShell with standard shell utilities.

PS> Set-Location -Path (Split-Path (Get-MailboxDatabase -Name MyExchangeDatabase).EdbFilePath)
PS> Dismount-Database -Name MyExchangeDatabase
PS> eseutil /d MyExchangeDatabase.edb /
PS> Mount-Database -Name MyExchangeDatabase

Note that when you start defragmentation on a database, your users cannot access any mailboxes stored in that database. Additionally, you will need about 110 percent the size of the database on disk to perform the defragmentation.

Conclusion

Although you can do many of the operations via the Exchange Admin Center, a best practice to administration is that of automation and repeatability. By leveraging PowerShell, you can limit common admin repetitions through automating many common scenarios, ultimately saving time and preventing headaches!

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!