Extract files and folders from SharePoint 2013 document library to local folder

john john 946 Reputation points
2022-04-08T17:19:24.817+00:00

We have a SharePoint document library inside SharePoint 2013 on-premises. the document library contains around 1 TB of files and folders. now we want to extract those files and folders from SharePoint 2013 to local folder? so is there any server-side code, client side code or Power shell code i can use to copy those files and folders and preserve the folder structure and preserve the files and folders metadata such as Createdby,Modifiedby,Created & Modified will be nice to have ?

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,594 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,881 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,141 Reputation points
    2022-04-11T09:17:57.443+00:00

    Hi @john john :
    Based on my research and testing, please try to use the following PowerShell script to download files and folders from the document library to a local folder:

     Add-PSSnapin Microsoft.Sharepoint.Powershell  
     $SiteURL = "http://sp/xxx"  
     $LibraryName ="library"  
     $DownloadPath ="C:\library"  
     Function Download-SPFolder($SPFolderURL, $DownloadPath)  
     {  
          Try {  
              $SPFolder = $web.GetFolder($SPFolderURL)  
              Write-host $SPFolder  
              $DownloadPath = Join-Path $DownloadPath $SPFolder.Name   
              If (!(Test-Path -path $DownloadPath))  
              {     
                  $LocalFolder = New-Item $DownloadPath -type directory  
              }  
              ForEach ($File in $SPFolder.Files)  
              {  
                  #Download the file  
                  $Data = $File.OpenBinary()  
                  $FilePath= Join-Path $DownloadPath $File.Name  
                  [System.IO.File]::WriteAllBytes($FilePath, $data)  
                  Write-host -f Green "`tDownloaded the File:"$File.ServerRelativeURL          
              }  
              ForEach ($SubFolder in $SPFolder.SubFolders)  
              {  
                  If($SubFolder.Name -ne "Forms")  
                  {  
                      #Call the function Recursively  
                      Download-SPFolder $SubFolder $DownloadPath  
                  }  
              }  
          }  
          Catch {  
              Write-host -f Red "Error Downloading Document Library:" $_.Exception.Message  
          }   
     }  
     #Main Function  
     Function Download-SPDocumentLibrary($SiteURL, $LibraryName, $DownloadPath)  
     {  
          Try {  
              #Get the  Web  
              $Web = Get-SPWeb $SiteURL  
              #Delete any existing files and folders in the download location  
              If (Test-Path $DownloadPath) {Get-ChildItem -Path $DownloadPath -Recurse| ForEach-object {Remove-item -Recurse -path $_.FullName }}  
              #Get the document Library to Download  
              $Library = $Web.Lists[$LibraryName]  
              Write-host -f magenta "Downloading Document Library:" $Library.Title  
              #Call the function to download the document library  
              Download-SPFolder -SPFolderURL $Library.RootFolder.Url -DownloadPath $DownloadPath  
              Write-host -f Green "*** Download Completed  ***"  
          }  
          Catch {  
              Write-host -f Red "Error Downloading Document Library:" $_.Exception.Message  
          }   
     }  
     Download-SPDocumentLibrary $SiteURL $LibraryName $DownloadPath  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    1 person found this answer helpful.