Export/Import document library using SharePoint Management PowerShell in SharePoint Server 2019

Tevon2.0 1,101 Reputation points
2023-01-17T17:24:48.3+00:00

(SharePoint Server 2019)

I would like to export all document libraries/lists from my old "NG-Portal" site on our old web application and import it into the new "NGPortal" site on our current web application using SharePoint Management PowerShell. However, I am unsure on the best layout for the commands. Could someone assist with the best command sequence to achieve this request. My initial thoughts were to use something like this below to start however I need clarification on what should be inserted for Web URL, back up path and Item URL?

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

The Export-Import all lists and libraries from one site to another site using PowerShell guidance isn't that clear as to which sections need to be filled in with my own data and what is part of the template.

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 "https://sales.crescent.com/" "D:\Backup\"
#To import, Use:
#Import-AllLists "https://marketing.crescent.com/" "D:\Backup\"

Also, if I did just do a standard library import/export via central admin can permissions be kept?

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,298 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,597 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,900 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Luigi Bruno 316 Reputation points
    2023-11-09T20:21:35.1033333+00:00

    Hello Tevon2.0.

    Web-Url is the URL of the SharePoint site, Backup-Path is the full path to the folder where the files created by the export operations are saved and Library-URL is the URL of the list/library you would like to export.
    I would suggest you to try with a single list or library before iterating on all the lists and libraries.

    Bye.

    0 comments No comments