SharePoint 2010 Powershell script to restore a Site Collection from a Backup.

SharePoint 2010 Powershell script to restore a Site Collection Backup

Here is my script I wrote to do a restore from a Site Collection Backup.
This will create a new content database for the site collection.  It made sense for us to do this because each site collection was a military unit that could be redeployed and they might want to take the database with them.

Since I was re-running my script, (and timing the job) I started out by deleting the site collection. 
Unless you're doing the same, you will not need this:

Get-SPContentDatabase -Identity "WSS_Content_SiteCollection1" | Remove-SPContentDatabase -Confirm:$false

Note: That's all one line ^

New-SPContentDatabase -Name "WSS_Content_SiteCollection1" -WebApplication http://sharepoint.name

Note: That's also all one line ^
Wait, did I just delete a site collection only to add it back???  Yes I did.  As I was running the restore job multiple times I wanted to be sure the previous attempt was gone.  Using this method will ensure that. Using the GUI (Central Admin) to delete a site collection is not recommended.  You'll find you won't be able to add it a second time because the old references don't get removed and the site collection is still in a site collection recycle bin.  If you remove it from the recycle bin that still might not help.

This displays no output, so I added some to tell me the time this started and when it finished. The rest of the script script is below.

$StartDate = Get-Date; "Restoring SiteCollection1"
Restore-SPSite -Identity http://sharepoint.name/sitecollection1 -Path \\UNCPATH\to\save\location\sitecollection1.bak -force -databasename "WSS_Content_SiteCollection1" -Confirm:$false
$EndDate = Get-Date; "SiteCollection1: Started at $StartDate and Completed at $EndDate"

The optional -force command will allow overwrite.  Since I know my content database is blank I don't need it, but an old guy named Ben told me to, so I used it anyway. 

The optional database name is a good idea as that tells the script exactly where you want to restore that site collection. Don't trust the GUI for this one either.  I've had a site collection restore to the wrong database when Central Admin told me that only the right database was "on" and the rest were marked as "off" in CA.

 

Comments

Popular posts from this blog

Setting SharePoint announcements to auto delete after expiration

Updating a single field in a SharePoint List using Power Automate Flows

SharePoint driven rich text dashboard using jqueryui. (JQuery file)