Moving a SharePoint 2010 Web using PowerShell

Moving a SharePoint 2010 Web using PowerShell

One of the tasks I have at my workplace is to “migrate” SharePoint sites from one parent site to another as projects are closed out.  I wanted a solution to keep all of the business’s active project sites separate from their “archived” ones.  The problem arose when that “archive” has to happen.  You can’t just “move” a website in SP2010 (or at least, not in any way I’ve found or researched).  You can, however, export and import the site, so that’s what I did.  However, given the fact Microsoft will be moving away from STSADM in future builds of SharePoint, now was as good a time as any to start applying PowerShell.  Here’s what I came up with (save the following as a .ps1 file and run in the SharePoint 2010 Management Shell):
cls
# Prompt the user for the source project name URL fragment
#
$input = Read-Host "Enter the website (ex: http://portal/site)"
#
# Set variables
#
$sourceURL = $input
$web = Get-SPWeb $sourceURL
$web = $sourceWeb.Name
$targetURL = "http://portal/archive/"+$sourceName
$title = $web.Title
#
# Let's begin
#
Write-Host "Extracting source URL"
#
# Export the source site to the desktop
#
Export-SPWeb $sourceURL -Path "C:userspublicdocumentstemp.cmp" -Force -IncludeUserSecurity -IncludeVersions all -NoFileCompression -NoLogFile
#
# In PowerShell, we need to create the website first, then import it. This is a by-design security feature
# built into PowerShell.
#
Write-Host "Migrating site to target URL"
New-SPWeb $targetURL -Template "STS#0"
#
# Update the target title
#
$web = Get-SPWeb $targetURL
$web.Title = $title
$web.Update()
#
# Import the same site, overwriting the existing one we just made
#
Import-SPWeb $targetURL -Path "C:userspublicdocumentstemp.cmp" -UpdateVersions Overwrite -Force -IncludeUserSecurity -NoFileCompression -NoLogFile
Write-Host "Removing source site"
#
# Remove the source site
#
Remove-SPWeb $sourceURL -Confirm:$false
Write-Host "Completed"

This exported everything into a temporary location, including versioning and user security, created the target website using the default Template (STS#0 — you can substitute for whatever template you’re using, just make sure they match), then imports to the target location, overwriting everything and including original user security and versioning, and finally removes the original site.

I’m sure someone’s thought of a better or more efficient way of doing this, but this one is quick and works for me.  Hope it helps someone!

Comments are closed.