Copy-SPSite: Clone SharePoint Site Collection using PowerShell
The Copy-SPSite is a new feature introduced in SharePoint 2013 that allows us to copy an existing site collection into a new site collection. Copy-SPSite cmdlet clones/duplicates an existing site collection into a new one. Optionally, You can specify the “Destination Database” parameter to copy the site collection into a new database.
Copy-SPSite Example:
Syntax:
Copy-SPSite {Source-Site-Collection-URL} [-DestinationDatabase {Destination-Database-Name}] -TargetUrl {Target-Site-Collection-URL}
Example:
Copy-SPSite https://intranet.crescent.com/sites/sales -TargetUrl https://intranet.crescent.com/sites/sales2 -DestinationDatabase SP13_Intranet_ContentDB005
This gets you a duplicate of a site collection on a new URL!
Using Copy-SPSite cmdlet to rename existing site collection:
To rename site collections, we followed the backup-delete-restore process as in my other article: Rename site collection in SharePoint 2010 using PowerShell. Of course, there is a risk involved as you delete site collections – There are chances to lose data. Fortunately, SharePoint 2013 came with new functionality, “Copy-SPSite”. As the name suggests, It’s used to copy the SharePoint site under a new name (URL) without any risk of losing data!
Copy SharePoint site to another web application
While it’s straightforward to copy the site to different site collections, I had a few glitches while copying the SharePoint site to another web application. Copy SharePoint site to another web application using Copy-SPSite doesn’t work!
Copy-SPSite : The site collection being moved and the destination content database must be within the same Web application.
Solution:
Use Backup-SPSite and Restore-SPSite cmdlets!
Copying Host Named Site Collections
I tried to copy a SharePoint site to another on path-based site collections, and It went through well. But when I tried copying a host-named site collection, I ended up with an error!
Copy-SPSite -Identity 'https://sales.crescent.com' -TargetUrl 'https://sales2.crescent.com' -HostHeaderWebApplication 'https://hosting.com'
And got this error:
Copy-SPSite : Another site already exists at https://sales.crescent.com/. Delete this site before attempting to create a new site with the same URL, choose a new URL, or create a new inclusion at the path you originally specified.
Workaround: Remove site URL and then run copy site!
Remove-SPSiteURL -URL https://your-hnsc-site-url
i, I tried to rename site using copy-spsite command, the command ran with no errors but there was no new site created. If I check Central Admin, I can see the new site url there, but it has no information and I cannot select and delete the site.
I tried deleting using PS (remove-spsite), no luck. How do I delete this orphaned site?
To delete orphaned site collection, use: How to Delete Orphan Site Collections in SharePoint
Very important notice at the bottom:
You can’t copy a site collection in SharePoint 2007/SharePoint 2010/SharePoint 2013 within the same content database