PowerShell for SharePoint (on-prem)

Copy a Site Collection to a new database using PowerShell

It’s pretty common in the SharePoint world to move Site Collection(s) from one database to another.
But there may also be some scenarios where you need to copy a Site Collection (SC) from one content database (CDB) to another.

In this blog post, we will focus on that by using the Copy-SPSite cmdlet.
If you prefer to watch a video, don’t hesitate to browse my YouTube channel, or particularly how to Copy a Site Collection to a new content database using PowerShell.


IMPORTANT:
When you wish to copy a Site Collection, the Site Collection being moved, AND the destination content database must be within the same Web Application.
Otherwise, you’ll need to use the Backup-SPSite & Restore-SPSite cmdlets.

 

Minimum requirements:

  • Securityadmin fixed server role on the SQL Server instance,
  • db_owner fixed database role on all databases that are to be updated,
  • Administrators group on the server on which you are running the PowerShell cmdlets,
  • New database must already exist (if you choose to copy to another database)

 

For testing purposes, I have created 1 site collection & 1 new content database within the same Web Application as follow:

  • Site Collection SOURCE (http://portal.sptradi.com/sites/scsource)
  • SP2016_NewTestCDB (using the New-SPContentDatabase)

 

Check the Source Site Collection & destination content database are within the same Web Application

  • Open SharePoint Management Shell (or PowerShell and add the SharePoint snap-in)
  • Type the following cmdlet (where “portal” is your own root URL)

 

Get-SPSite | Where-Object {$_.Url -like "http://portal*"} | select Url, ContentDatabase, WebApplication
scsource_img
Site Collection SOURCE

 

Get-SPContentDatabase -WebApplication http://portal.sptradi.com/
NewCDB_img
New Content Database on same Web Application

We can clearly see that the Site Collection SOURCE and the new content database are on the same Web Application (http://portal.sptradi.com/)

 

So let’s copy this SC into a new content database.

scsource_img

Current SOURCE Site Collection

To copy the SOURCE (with content) to the TARGET, run the following (replace with your own URL):

Copy-SPSite -Identity http://portal.sptradi.com/sites/scsource -DestinationDatabase SP2016_NewTestCDB -TargetUrl http://portal.sptradi.com/sites/sctarget
sccopied_img
Site Collection SOURCE successfully copied

 

Verification

Get-SPSite | Where-Object {$_.Url -like "http://portal*"} | select Url, ContentDatabase, WebApplication
results
Site Collection TARGET on new database

 

CDBinSP
Number of Site Collections on each database

 

Navigate to the TARGET site collection and look at the results! (note: the title will need to be changed)

sctargetinSP
Site Collection copied in TARGET

 

 

Loading Likes... Tagged , , , ,