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 }


  1. Spared me alot of time - thanks!

  2. Maybe I was a little to fast with the praise :)

    You need to change the Import part.
    This works:

    $mysitesurl = "http://portal/my/personal/"
    $backuppath = "C:\MySitesBackup\"
    $files = get-childitem $backuppath |
    ForEach-object { restore-spsite ($mysitesurl + $_.Name) -path ($backuppath + $_.Name) -Confirm:$false -verbose}

  3. After exploring a handful of the articles on your site,
    I really appreciate your way of writing a blog.
    I bookmarked it to my bookmark site list and will be checking back soon. Please check
    out my website as well and tell me what you think.

    my page - authentic green coffee