Monday, 9 January 2012

Exporting and importing all my site collections using PowerShell

Here is the PowerShell script that I used for exporting (backuping) and importing (restoring) all "my site" site collections. This script could be used as an alternative to STSADM mergecontentdbs command which I found a little bit inflexible. I used this script for moving my sites to a different web application.

Exporting my sites:
$mysitesurl = "http://portal/my/personal/*"
$backuppath = "c:\MySitesBackup\"
$webapp = "http://portal"
Get-SPSite -WebApplication $webapp -Limit ALL | where { $_.Url -like $mysitesurl } | ForEach-Object{ $path = $backuppath + $_.Url.Substring($_.Url.LastIndexOf("/")+1); Backup-SPSite -Identity $_.Url -Path $path -Verbose }



Importing my sites:
$mysitesurl = "http://portal/my/personal/*"
$backuppath = "c:\MySitesBackup\"
$webapp = "http://portal"
Get-SPSite -WebApplication $webapp | where { $_.Url -like $mysitesurl } | ForEach-Object{ $path = $backuppath + $_.Url.Substring($_.Url.LastIndexOf("/")+1); Backup-SPSite -Identity $_.Url -Path $path -Verbose -Force }