Recently we had a task at one of our clients to consolidate all of the site collections of a SharePoint web application into a single content database.
There were around 10 content DBs in this web application, each of them included about 5-10 site collections. The total size of the content DBs was around 1-2 GBs, and they expected no growth in the near future.
To make the farm administration easier we had to move all of the site collections into the first content DB (let’s call it ContentDB1). We achieved this goal via the following PowerShell script:
$webAppUrl = "http://YourSPWebApp"
$destinationDBName = "ContentDB1"
$wa = Get-SPWebApplication $webAppUrl
$destinationDB = Get-SPContentDatabase -WebApplication $wa | ? { $_.Name -eq $destinationDBName }
Get-SPContentDatabase -WebApplication $wa | ? { $_.Name -ne $destinationDBName } | % {
Get-SPSite -ContentDatabase $_ | Move-SPSite -DestinationDatabase $destinationDB -Confirm:$False
# we can disable the content DB at the end
Set-SPContentDatabase $_ -Status Disabled -Confirm:$False
# or dismount it from SharePoint
# Dismount-SPContentDatabase $_ -Confirm:$False
# or remove it from the SharePoint server as well as from SQL server
# Remove-SPContentDatabase $_ -Confirm:$False -Force
}
As you can see, we iterate through all site collections of all DBs in the web application (except the target content DB, ContentDB1), and move them into the ContentDB1 database using the Move-SPSite CmdLet.
At the end we can either disable the already empty content databases, dismount the from SharePoint, or remove them from the SQL server.
Don’t forget to execute IISRESET after the script to let the configuration changes be reflected in the Central Administration UI.
