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.


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.

#Function to Rename Database in SQL Server
function Rename-SQLDatabase($DBServerName, $OldDatabaseName, $NewDatabaseName)
        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

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

        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)
        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

        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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time 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.

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

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


Leave a Reply

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