Copy SharePoint List or Library between Sites with PowerShell
In a migration project, we had to move a few lists and libraries to a different site collection. In SharePoint 2010, using granular export, we can export SharePoint lists and libraries from one site to another with the SharePoint Central Administration web interface.
Once exported we’ve to use Import-SPWeb PowerShell cmdlet to restore the list. Lists can be copied to different SharePoint sites as well.
Exporting from Central Admin UI and importing it again isn’t that great time-saver, So let’s turn our attention to PowerShell to copy SharePoint 2010 list between sites. Here is my PowerShell script to copy a single List or Library to another SharePoint Site:
Export-Import a Library using PowerShell:
Export-SPWeb "https://your-site-url" -itemurl "/document" -path "c:\documents.cmp" -IncludeVersions All
Import-SPWeb "https://your-new-site-url/" -Path "c:\documents.cmp"
PowerShell Script to Backup-Restore (Or Export-Import) SharePoint List:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Function to copy List or Library from One SharePoint site to another
Function CopyList([string]$SourceWebURL, [string]$TargetWebURL, [string]$ListName, [string]$BackupPath)
{
#Get the Source List
$SourceList =(Get-SPWeb $SourceWebURL).Lists[$ListName]
#Export the List from Source web
Export-SPweb $SourceWebURL -ItemUrl $SourceList.DefaultViewUrl -IncludeUserSecurity -IncludeVersions All -path ($BackupPath + $ListName + ".cmp") -nologfile
#Import the List to Target Web
import-spweb $TargetWebURL -IncludeUserSecurity -path ($BackupPath + $ListName + ".cmp") -nologfile
}
#Call the function
CopyList "https://sharepoint.crescent.com" "https://sharepoint.crescent.com/teams/marketing" "Banner" "C:\Temp\"
When I had a bunch of Lists to move from one site to another, I Just placed them in an array and called the function like this:
$Lists = @("Project Docs", "Project Metrics", "Milestones")
foreach($List in $Lists)
{
#Call the function to copy
CopyList "https://sharepoint.crescent.com" "https://sharepoint.crescent.com/teams/marketing" $List "C:\Temp\"
}
Write-Host "Completed!"
In an another situation, I had to copy of all Lists and libraries from a one Site Collection to another Site Collection. To export a single list use:
Export-SPweb "https://portal.crescent.com/News" -ItemUrl "Lists/Org News" -IncludeUserSecurity -IncludeVersions All -path "C:\Reports\News.cmp" -nologfile
PowerShell Script to Copy All Lists from One Site to Another
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Function to copy List or Library from One SharePoint site to another
Function CopyList([string]$SourceWebURL, [string]$TargetWebURL, [string]$ListName, [string]$BackupPath)
{
#Get the Source List
$SourceList =(Get-SPWeb $SourceWebURL).Lists[$ListName]
#Export the List from Source web
Export-SPweb $SourceWebURL -ItemUrl $SourceList.DefaultViewUrl -IncludeUserSecurity -IncludeVersions All -path ($BackupPath + $ListName + ".cmp") -nologfile -Force
#Import the List to Target Web
import-spweb $TargetWebURL -IncludeUserSecurity -path ($BackupPath + $ListName + ".cmp") -nologfile -UpdateVersions Overwrite
}
#Get All List Names
$Lists = @($(Get-SPWeb $SourceWebURL).lists)
foreach($List in $Lists)
{
#Leave the Hidden Lists and exclude certain Libraries
if($List.Hidden -eq $false -and $List.Title -ne "Style Library" -and $List.Title -ne "Site Pages")
{
#Call the function to copy
CopyList "https://sharepoint.crescent.com" "https://sharepoint.crescent.com/HR/" $List.Title "C:\Temp\"
}
}
Write-Host "Completed Copying Lists!"
These scripts copies SharePoint list to another site including security and versions. Saving list as template with content, Export to spreadsheet are the OOTB methods for copying lists between site collections.
Thanks for this script ! Can we apply this to copy multiple lists from SharePoint 2016 to Online ?
great work
great work
Great Script – thanks a lot!
Nice article. FYI -The initial CopyList function script has the export command commented out.