How to Rename SharePoint 2013 Content Databases with PowerShell?

Requirement: To follow standard naming conventions, We got to rename SharePoint content databases in our SharePoint 2013 environment.

Solution:

You can rename a SharePoint content database in these three steps as explained in my other post: How to Rename SharePoint 2013/2010 Central Admin Database and Remove GUID?

  1. Detach SharePoint content database from SharePoint web application
  2. Rename the content database in SQL Server
  3. Attach the renamed content database back to SharePoint
rename sharepoint content database powershell

With the help of PowerShell, let’s automate these manual steps.

Rename SharePoint content database with PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Load the assemblies required for the SQL database rename.
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") 
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended")

#Function to Rename Database in SQL Server
function Rename-SQLDatabase($DBServerName, $OldDatabaseName, $NewDatabaseName)
{
    try
    {
        Write-host Establishing connection with SQL Server... -foregroundcolor "Yellow"
        #Connect to the server
        $Server = new-Object Microsoft.SqlServer.Management.Smo.Server($DBServerName)
        
        Write-host Getting the Database in SQL Server... -foregroundcolor "Yellow"
        #Get the database
        $Database = $Server.Databases.Item($OldDatabaseName)

        #Kill all active connections to the SQL database
        $Server.KillAllprocesses($OldDatabaseName)

        Write-host Renaming  Database in SQL Server... -foregroundcolor "Yellow"
        #Rename the database
        $Database.Rename($NewDatabaseName)
        Write-host Database Renamed from $OldDatabaseName to $NewDatabaseName in SQL Server -ForegroundColor Green
    }

    catch
    {
        Write-Error $_.Exception.Message
        Write-Error "Error in Renaming Database in SQL Server!" 
    }
}

#Function to Rename content database in SharePoint
function Rename-ContentDatabase($OldDBName, $NewDBName)
{
    try
    {
        Write-host Getting SharePoint Content Database ... -foregroundcolor "Yellow"
        $ContentDB = Get-SPContentDatabase | Where-object {$_.Name -eq $OldDBName}

        #Get Content Database settings
        $WebApp = $ContentDB.WebApplication.Url
        $DBServer = $ContentDB.Server
        $MaximumSites = $ContentDB.MaximumSiteCount
        $WarningSites = $ContentDB.WarningSiteCount

        #Dismount Content Database
        Write-host Dismounting Content Database ... -foregroundcolor "Yellow"
        Dismount-SPContentDatabase $OldDBName -Confirm:$False

 
        Write-host Renaming Database: $OldDBName to $NewDBName in SQL Server -foregroundcolor "Yellow"

        #Call function to rename Database in SQL Server 
        Rename-SQLDatabase $DBServer $OldDBName $NewDBName

        Write-host Mounting SharePoint content Database... -foregroundcolor "Yellow"
        #Mount the database back
        Mount-SPContentDatabase -Name $NewDBName -WebApplication $WebApp -DatabaseServer $DBServer -MaxSiteCount $MaximumSites -WarningSiteCount $WarningSites | out-null

        Write-host Done!Content Database Renamed from $OldDBName to $NewDBName!! -ForegroundColor Green
    }
     catch
    {

        Write-Error $_.Exception.Message
    }
}

#Call the function to rename database
Rename-ContentDatabase "WSS_Content_310d122490c4303b1c0b3f2f695e7" "HostingFarm_Content_Hosting01" 

Alternate approach: Create a content database with the desired name, move all site collections from the existing database to the new database using the cmdlet Move-SPSite and then dismount the old content database.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “How to Rename SharePoint 2013 Content Databases with PowerShell?

  • Getting error on SQL rename from WFE:
    Rename-SQLDatabase : Cannot find type [Microsoft.SqlServer.Management.Smo.Server]: verify that the assembly containing this type is loaded.

    Reply
  • this script is exactly what I need, thank you!

    Reply
  • will attaching and deattaching cause any issues like in workflows running etc..

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *