Mount-Dismount (Attach-Detach) Content Databases in SharePoint using PowerShell

There are occasions when you may need to attach or detach a content database to SharePoint web applications. E.g., during migrations, you want to restore a content database from another farm and would like to access the sites in the content database from a new web application. You can attach or detach SharePoint 2016 content databases using Central Administration or PowerShell.

As stsadm is deprecated in SharePoint 2013, We should use the below PowerShell cmdlets to attach-detach content databases in SharePoint:

Mount Dismount Content Databases in SharePoint using PowerShell


Mount-SPContentDatabase

The Mount-SPContentDatabase cmdlet attaches an existing content database to the specified Web application in the SharePoint farm. If the database
mounted requires an upgrade, and the cmdlet upgrades the database automatically. We use this cmdlet when:

  • During Migrations when you want to attach (or associate) the content database with a new build (or new platform). E.g. Database attach upgrade
  • Move a content database from one database server to another, E.g. You got the database backup of a SharePoint site from your client and want to use that database in your environment
  • When we attach a content database to a different web application.
Before using Mount-SPContentDatabase, make sure you restored the database to your SharePoint Database Server (SQL Server) first!

Mount-SPContentDatabase syntax to attach a content database:

Mount-SPContentDatabase -Name "Content-database-name" -DatabaseServer "db-server-name" -WebApplication "Name-of-the-webapp"

E.g.

Mount-SPContentDatabase -name "SP2010_Intranet_Content" -DatabaseServer "G1-SP-SQL01" -WebApplication "https://migration.crescent.com" -confirm:$false

This PowerShell command attaches the content database “SP2010_Intranet_Content” to the SharePoint 2013 web application “https://migration.crescent.com”. By default, the Mount-SPContentDatabase cmdlet upgrades the database schema to the destination build and upgrades all site collections within the specified content database, if required!

To attach content database using STSADM, use:
stsadm -o addcontentdb -url https://SharePoint.company.com -databasename <MOSS2007_Migration_Content>

Dismount-SPContentDatabase

Dismount-SPContentDatabase is the reverse of Mount-SPContentDatabase, and it detaches the given content database from the specified web application. This cmdlet updates the mapping between web application and content database but doesn’t actually delete the database from the SQL server.

Dismount-SPContentDatabase syntax to detach content database:

Dismount-SPContentDatabase -Name "Content-database-name"

E.g., Here is how to detach the content database in SharePoint 2010

Dismount-SPContentDatabase -Name "SP2010_Intranet_Content"

This PowerShell cmdlet removes the content database from SharePoint. If you still want to use STSADM to detach a content database in SharePoint, use:
stsadm -o deletecontentdb -URL https://SharePoint.company.com -databasename <MOSS2007_Migration_Content> 

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

One thought on “Mount-Dismount (Attach-Detach) Content Databases in SharePoint using PowerShell

  • Post mounting to sharepoint 2016 all the login account of sp2013 are removed. Any reason

    Reply

Leave a Reply