How to Export/Import a Site using a PowerShell Script in SharePoint 2007 and SharePoint 2010

How to Export/Import a Site using a PowerShell Script in SharePoint 2007 and SharePoint 2010



Introduction

I recently published an article about exporting and importing a site using sharepoint object model. Someone recently asked me for doing the same via powershell scripts. Hence I have done that in this article. This scripts first exports a site and then ask for the decision to go for import or not. If you type "y" it will go or else it will not go, hence in this way it gives you to analyze the file sizes and all other factors before going to import a site.
Here is the script for this:

PowerShell Script


param([string]$Sourceurl,[string]$Destinationurl, [string]$FileName, [string]$Location, [switch]$help)


[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")


function GetHelp() {

$HelpText = @"

This script export a site from source site and then imports that at a destination url.
It has a check also before it goes for import.

"@
$HelpText


}


function RahulExportImport-SPSite([string]$Sourceurl,[string]$Destinationurl, [string]$FileName, [string]$Location) {


 if(Test-Path $Location) {


 } else {
  new-item -path $Location -type directory | Out-Null
 }


 $SPExport = New-Object Microsoft.SharePoint.Deployment.SPExport
 $SPImport = New-Object Microsoft.SharePoint.Deployment.SPImport
 $SPExportSettings = $SPExport.Settings
 $SPExportSettings.SiteUrl= $Sourceurl
 $SPExportSettings.BaseFilename = $FileName
 $SPExportSettings.FileLocation = $Location
 $SPExportSettings.CommandLineVerbose = $true
 $SPExportSettings.ExportMethod = [Microsoft.SharePoint.Deployment.SPExportMethodType]"ExportAll"
 $SPExportSettings.LogFilePath =$Location +"\logExport.log"
 
 $SPExport.Run()
 write-host "The export is completed. Do you want to go for Import? If Yes then type y or else type anything else"
 $Decision = read-host
 if($Decision -eq "y")
 {
 $SPImportSettings = $SPImport.Settings
 $SPImportSettings.SiteUrl= $Destinationurl
 $SPImportSettings.BaseFilename = $FileName
 $SPImportSettings.FileLocation = $Location
 $SPImportSettings.CommandLineVerbose = $true
 $SPImportSettings.LogFilePath =$Location +"\logImport.log"
 $SPImport.Run()
 }


 $SPExport.Dispose()
 $SPImport.Dispose()
       
}


if($help) { GetHelp; Continue }
if($Sourceurl -AND $Destinationurl -AND $FileName -AND $Location) { RahulExportImport-SPSite -Sourceurl $Sourceurl -Destinationurl $Destinationurl -FileName $FileName -Location $Location }


I hope this will help you out.


Thanks,
Rahul Rashu

Leave a Comment
  • Please add 5 and 6 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Fernando Lugão Veltem edited Revision 7. Comment: remove en-us from title

  • Craig Lussier edited Revision 6. Comment: added en-US to tags and title

  • Craig Lussier edited Revision 4. Comment: Minor edit to title

  • Craig Lussier edited Revision 3. Comment: Added headings, toc and SharePoint tag

  • Leonardo Côco edited Original. Comment: Edited and added tags

Page 1 of 1 (5 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Leonardo Côco edited Original. Comment: Edited and added tags

  • Craig Lussier edited Revision 3. Comment: Added headings, toc and SharePoint tag

  • You should also consider adding this one to the TechNet Gallery. Cheers.

  • Craig Lussier edited Revision 4. Comment: Minor edit to title

  • Hi Craig,

    Thanks for your comment. I have added tsi as well.

    The URL is gallery.technet.microsoft.com/ExportImportSiteUsingPowers-d22e2780

    Thanks,

    Rahul Rashu

  • Craig Lussier edited Revision 6. Comment: added en-US to tags and title

  • Hi Rahul,  

    I have some very large sites that I need to export from the existing site collection and move them to a new site collection. Some of them are as big as 32 GB. Can I use your script to export such large sites? If I have enough free space on the local drive or on a shared network drive can I still export these large sites and then import them into a new site collection?

    I am worried about the timeout issues or freezing of services due to such high volume of data. Basically those sites have document libraries with lots of big documents.

    Any assistance will be appreciated. We have MOSS 2007 x64 installed on Win 2008 servers.

    Thanks,

    Anant

  • Fernando Lugão Veltem edited Revision 7. Comment: remove en-us from title

Page 1 of 1 (8 items)