Weather you are performing a SharePoint migration or you simply want to restructure your current farm, PowerShell provides ways to reorganize/migrate your data without having to employ 3rd party tools.
When we think of data migration, as it pertains to SharePoint Information Architecture we think of Web Apps, Site Collections, and Webs. Maybe we want a current web to become a site collection? Maybe we want to move a web or make an existing site collection a web? These are all questions you must ponder during the planning of your data migration. We can get much more granular than this and go down the list and even item level, but my normal encounters with this type of requirement involve moving the top level SP objects rather then items and lists.
What should we do first?
First I create a matrix in Excel and export that matrix to CSV. This matrix is our master lookup table and rules list of what we want to do within our farm. See below for a sample of what I am talking about. I have put this into a table for easier viewing.
| Name | SourceURL | DestinationURL | Path | Owner | Object Type | Action | Template | |
| Finance | http://lab/sites/finance | http://lab/dept/fin | D:\Temp\fin.cmp | Lab\Administrator | admin@company.com | Web | Convert | BLANKINTERNET#0 |
| Support | http://lab/sites/support | http://lab/dept/support | D:\Temp\sup.cmp | Lab\Administrator | admin@company.com | Web | Convert | BLANKINTERNET#0 |
| Development | http://lab/sites/dev | http://lab/dept/support/dev | D:\Temp\dev.cmp | Lab\Administrator | admin@company.com | SiteCollection | Convert | STS#0 |
| News | http://lab/sites/news | http://lab/dept/news | D:\Temp\news.bak | Lab\Administrator | admin@company.com | SiteCollection | Move | NA |
| Sales | NA | http://lab/dept/sales | NA | Lab\Administrator | admin@company.com | SiteCollection | New | BLANKINTERNET#0 |
Note: Object Type and Action determine what function will be called on these objects.
Object Type = Web + Action = Convert = Convert Web to Site Collection
All of these options are things that can be added or removed for your scenario. The script you can use is below.
#Script
# Load Snapin and get script directory
add-pssnapin microsoft.sharepoint.powershell
#Logging functions courtesy of Brian Caauwe (MCM: SharePoint 2010)
function Write-ToScreen($msg)
{
$currentTime = Get-Date -UFormat "%H:%M:%S"
$msg = $currentTime + " :: INF :: " + $msg
Write-ToLog($msg)
Write-Host -ForegroundColor Yellow $msg
}
function Write-ActionToScreen($msg)
{
$currentTime = Get-Date -UFormat "%H:%M:%S"
$msg = $currentTime + " :: ACT :: " + $msg
Write-ToLog($msg)
Write-Host -ForegroundColor Green $msg
}
function Write-ErrorToScreen($msg)
{
$currentTime = Get-Date -UFormat "%H:%M:%S"
$msg = $currentTime + " :: ERR :: " + $msg
Write-ToLog($msg)
Write-Host -ForegroundColor Red $msg
}
function Write-ToLog($msg)
{
$logDate = Get-Date -UFormat "%m%d%Y"
$path = "SharePointReOrg-" + $logDate + ".log"
$msg | Out-File $path -append
}
function New-SiteCollections($DestinationURL,$Owner,$Name,$Template)
{
Start-SPAssignment
Write-ActionToScreen "Creating new site collection $Name"
$nsite = New-SPSite -Url $DestinationURL -OwnerAlias $Owner -Name $Name -Template $Template
Write-ToScreen "Finished creating $Name site collection"
Stop-SPAssignment -Global
}
function Convert-WebToSiteCollection($SourceURL,$Path,$DestinationURL,$Owner,$Name)
{
Write-ActionToScreen "Creating new site collection $Name"
$nsite = New-SPSite -Url $DestinationURL -OwnerAlias $Owner -Name $Name
Write-ToScreen "Finished creating $Name site collection"
Write-ActionToScreen "Backing up source web $SourceURL"
$eweb = Export-SPWeb -Identity $SourceURL -Path $Path -Force
Write-ToScreen "Finished backing up $SourceURL"
Write-ActionToScreen "Restoring backup $path to destination site collection $DestinationURL"
$iweb = Import-SPWeb -Identity $DestinationURL -Path $Path -Force
Write-ToScreen "Finished restoring web $SourceURL to desination site collection $DestinationURL"
Write-ActionToScreen "Deleting the backup file to save space"
Remove-Item D:\BackupLogs\*.cmp
Write-ToScreen "Finished deleting backup file."
Stop-SPAssignment -Global
}
function Convert-SiteCollectionToWeb($SourceURL,$DestinationURL,$Path,$Name)
{
Write-ActionToScreen "Exporting web $SourceURL to $path"
$eweb = Export-SPWeb -Identity $SourceURL -Path $Path -Force
Write-ToScreen "Finished exporting $DestinationURL to $path"
Write-ActionToScreen "Creating new web at $DestinationURL"
$nweb = New-SPWeb -Url $DestinationURL -Name $Name -AddToQuickLaunch
Write-ToScreen "Finished creating new web at $DestinationURL"
Write-ActionToScreen "Restoring data from $SourceURL into new web $DestinationURL"
$iweb = Import-SPWeb -Identity $DestinationURL -Path $Path -Force
Write-ToScreen "Finished restoring web $SourceURL to destination site collection $DestinationURL"
Write-ActionToScreen "Deleting the backup file to save space"
Remove-Item D:\BackupLogs\*.cmp
Write-ToScreen "Finished deleting backup file."
Stop-SPAssignment -Global
}
function Move-Web($SourceURL,$DestinationURL,$Path,$Name)
{
Start-SPAssignment
Write-ActionToScreen "Exporting web $SourceURL to $path"
$eweb = Export-SPWeb -Identity $SourceURL -Path $Path -Force
Write-ToScreen "Finished exporting $DestinationURL to $path"
Write-ActionToScreen "Creating new web at $DestinationURL"
$nweb = New-SPWeb -Url $DestinationURL -Name $Name -AddToQuickLaunch
Write-ToScreen "Finished creating new web at $DestinationURL"
Write-ActionToScreen "Restoring data from $SourceURL into new web $DestinationURL"
$iweb = Import-SPWeb -Identity $DestinationURL -Path $Path -Force
Write-ToScreen "Finished restoring web $SourceURL to destination site collection $DestinationURL"
Write-ActionToScreen "Deleting the backup file to save space"
Remove-Item D:\BackupLogs\*.cmp
Write-ToScreen "Finished deleting backup file."
Stop-SPAssignment -Global
}
function Move-SiteCollection($SourceURL,$DestinationURL,$Path,$Name)
{
Start-SPAssignment
Write-ActionToScreen "Backing up site collection $SourceURL"
$bsite = Backup-SPSite -identity $SourceURL -path $Path -force
sleep 4
Write-ToScreen "Finished backing up site collection $SourceURL to $Path"
Write-ActionToScreen "Restoring $Path to $DestinationURL"
$rsite = Restore-SPSite -identity $DestinationURL -path $Path -force -Confirm:$false
Write-ToScreen "Finished restoring $path to $DestinationURL"
Write-ActionToScreen "Deleting the backup file to save space"
Remove-Item D:\BackupLogs\*.bak
Write-ToScreen "Finished deleting backup file."
Stop-SPAssignment -Global
}
function New-Webs($DestinationURL,$Name,$Template)
{
Start-SPAssignment
Write-ActionToScreen "Creating New Web $DestinationURL"
$nweb = New-SPWeb -Url $DestinationURL -Name $Name -Template $Template -AddToQuickLaunch
Write-ToScreen "Finished creating web $Name at $DestinationURL"
Stop-SPAssignment -Global
}
$SiteInfo = Import-Csv .\siteinfo.csv
$tranpath = "SharePointReOrgTranscript-" + $logDate + ".log"
Start-Transcript -Path .\$tranpath -Append
#Create New Site Collections
Write-ActionToScreen "CREATE NEW SITE COLLECTIONS"
$NewSites = $SiteInfo | where { $_.Action -eq "New" -and $_.Type -eq "SiteCollection" }
foreach ($site in $NewSites)
{
New-SiteCollections $site.DestinationURL $site.OwnerAlias $site.Name $site.Template
}
#Move Site Collections
Write-ActionToScreen "MOVE EXISTING SITE COLLECTIONS"
$MoveSites = $SiteInfo | where { $_.Action -eq "Move" -and $_.Type -eq "SiteCollection" }
foreach ($site in $MoveSites)
{
Move-SiteCollection $site.SourceURL $site.DestinationURL $site.SavePath $site.Name
}
#Create New Webs
Write-ActionToScreen "CREATING NEW WEBS"
$NewWebs = $SiteInfo | where { $_.Action -eq "New" -and $_.Type -eq "Web" }
foreach ($web in $NewWebs)
{
New-Webs $web.DestinationURL $web.Name $web.Template
}
#Convert Webs to Site Collection
Write-ActionToScreen "CONVERTING WEBS TO SITE COLLECTIONS"
$ConvertWebs = $SiteInfo | where { $_.Action -eq "Convert" -and $_.Type -eq "Web" }
foreach ($Web in $ConvertWebs)
{
Convert-WebToSiteCollection $Web.SourceURL $Web.SavePath $Web.DestinationURL $web.OwnerAlias $web.Name
}
#Convert Site Collections to Webs
Write-ActionToScreen "CONVERT SITE COLLECTIONS TO WEBS"
$ConvertSites = $SiteInfo | where { $_.Action -eq "Convert" -and $_.Type -eq "SiteCollection" }
foreach ($site in $ConvertSites)
{
Convert-SiteCollectionToWeb $site.SourceURL $site.DestinationURL $site.SavePath $site.Name
}
#Move Webs
Write-ActionToScreen "MOVING EXISTING WEBS"
$MoveWebs = $SiteInfo | where { $_.Action -eq "Move" -and $_.Type -eq "Web" }
foreach ($web in $MoveWebs)
{
Move-Web $web.SourceURL $web.DestinationURL $web.SavePath $web.Name
}
Stop-Transcript
{Kam}