Created
February 11, 2019 20:01
-
-
Save poiriersimon/0bdd990ba6f7e20a6fdd32e59f4fcb80 to your computer and use it in GitHub Desktop.
Remove Database with no mailboxes for Exchange 2010+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$MailboxDatabase = Get-mailboxDatabase | where {$_.Recovery -eq $False} | |
$EmptyDBs = @() | |
foreach($DB in $MailboxDatabase){ | |
$Mailboxes = $DB | Get-Mailbox | select Identity -first 1 | |
if($Mailboxes -eq $NULL){ | |
$EmptyDB = New-Object PSObject | |
$EmptyDB | Add-Member NoteProperty -Name "Name" -Value $DB.Name | |
$EmptyDB | Add-Member NoteProperty -Name "EDBPath" -Value $DB.EdbFilePath | |
$EmptyDB | Add-Member NoteProperty -Name "LogPath" -Value $DB.LogFolderPath | |
$EmptyDB | Add-Member NoteProperty -Name "Servers" -Value $($DB.Servers -join ',') | |
$EmptyDBs += $EmptyDB | |
Write-host "Remove Mailbox Database Copy $($DB.name)" | |
[array]$PassiveDBs = Get-MailboxDatabaseCopyStatus $DB.name | where {$_.status -eq "Healthy"} | |
foreach($PassiveDB in $PassiveDBs){ | |
Remove-MailboxDatabaseCopy $PassiveDB.name -confirm:$False | |
} | |
Start-Sleep 30 | |
Write-host "Remove Database $($DB.name)" | |
Remove-MailboxDatabase $EmptyDB.Name -confirm:$False | |
Start-sleep 30 | |
write-host "Remove Folders" | |
foreach($Server in $DB.Servers){ | |
$DBPath = "\\$($Server.tostring())\$($(Split-Path $EmptyDB.EDBPath.PathName).replace(':','$'))" | |
$LogPath = "\\$($Server.tostring())\$($($EmptyDB.LogPath.PathName).replace(':','$'))" | |
Remove-Item $DBPath -force -Recurse | |
Remove-Item $LogPath -force -Recurse | |
} | |
} | |
} | |
$EmptyDBs | export-csv -NoTypeInformation C:\Temp\EmptyDatabaseInfo.csv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment