question

johnjohn-0472 avatar image
0 Votes"
johnjohn-0472 asked TongZhangMSFT-7548 commented

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

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 ?

office-sharepoint-server-administrationoffice-sharepoint-server-development
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TongZhangMSFT-7548 avatar image
0 Votes"
TongZhangMSFT-7548 answered TongZhangMSFT-7548 commented

Hi @johnjohn-0472:
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.




· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @johnjohn-0472,
I am checking to see if the problem has been resolved .If you have any questions you can feel free to contact me.

0 Votes 0 ·

Hi @johnjohn-0472,
Would you tell me whether your issue has been resolved or have any update?

I am looking forward to your reply.

Thank you for your understanding and support.

0 Votes 0 ·