Export-Import All Lists and Libraries from One Site to Another using PowerShell

The requirement is to copy and paste all SharePoint lists and libraries from one site to another. While “Save List as a Template” is one approach, it doesn’t work for larger lists, and it is time-consuming. Using the Central Administration export method also kills my time.

export import sharepoint list using powershell

Solution: We can copy and paste SharePoint lists using PowerShell script.

Export document library using PowerShell in SharePoint 2013:

Syntax:
Export-SPWeb <Web-URL> -Path <Backup-Path> -ItemURL <Library-URL> -IncludeUserSecurity  -IncludeVersions All

E.g.

Export-SPWeb http://sharepoint.crescent.com/Teams/2015-ESG/ -Path "D:\Documents.cmp" -ItemURL "Documents" -IncludeUserSecurity -IncludeVersions All 

Import document library in SharePoint 2013 using PowerShell:

Syntax:
Import-SPWeb <<web-URL>> -path <<Backup-file>> -force -IncludeUserSecurity -UpdateVersions Overwrite

E.g.

Import-SPWeb https://sharepoint.crescent.com/Teams/2016-ESG/" -Path "D:\Documents.cmp" -force -IncludeUserSecurity -UpdateVersions Overwrite

Export-Import all lists and libraries from one site to another site using PowerShell:

Use this PowerShell script to export all lists from a site

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Custom PowerShell Function to Export All Lists and Libraries from a SharePoint site
Function Export-AllLists($WebURL, $ExportPath)
{
   #Get the source web
   $web = Get-SPWeb $WebURL

   #Check the Local Folder export Lists
    
   #Get all lists - Exclude System lists
   $ListCollection = $web.lists | Where-Object  { ($_.hidden -eq $false) -and ($_.IsSiteAssetsLibrary -eq $false) -and ($_.Author.LoginName -ne "SHAREPOINT\system") }

   #Iterate through each list and export
   foreach($list in $ListCollection)
    {
        Write-host "Exporting: " $list.Title
        #Remove : from List title - As we can't name a file with : symbol
        $ListTitle = $list.Title.Replace(":",[string]::Empty)
        Export-SPWeb $WebURL -ItemUrl "/$($list.RootFolder.Url)" -IncludeUserSecurity -IncludeVersions All -path ($ExportPath + $ListTitle+ ".cmp") -nologfile
    } 
}

#Custom PowerShell Function to Export All Lists and Libraries from a SharePoint site
Function Import-AllLists($WebURL, $ImportPath)
{
   #Get the Target web
   $web = Get-SPWeb $WebURL

   #Check the Local Folder export Lists
    
   #Get all File Backups to Import
   $FilesCollection = Get-ChildItem $ImportPath

   #Iterate through each file and import
   foreach($File in $FilesCollection)
    {
        Write-host "Importing: " $File.Name
        Import-SPWeb $webURL -path $ImportPath$File -includeusersecurity -UpdateVersions Overwrite -nologfile
    } 
}

#Call the function to export
Export-AllLists "http://sales.crescent.com/" "D:\Backup\"
#To import, Use:
#Import-AllLists "http://marketing.crescent.com/" "D:\Backup\"

This script helped me to move all lists from a highly customized and corrupted site to a clean SharePoint site. Here is another PowerShell script to copy-paste all lists and libraries between Sites: Copy SharePoint List or Library between Sites with PowerShell

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

One thought on “Export-Import All Lists and Libraries from One Site to Another using PowerShell

  • May 5, 2021 at 1:33 AM

    Tried above powersheel but getting error as “Export-SPWeb : The URL provided is invalid. Only valid URLs that are site collections or sites are allowed to be exported using stsadm.exe.”

    Reply

Leave a Reply