Downloading all items in a SharePoint On-Prem Document Library using PowerShell

 


Use this PowerShell script to download all items in a SharePoint document library. This only works for on-prem SharePoint.

Source: https://docs.microsoft.com/en-us/answers/questions/306049/download-complete-folders-and-subfolders-in-sharep.html

  1. Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  2. #Runtime-Variables
  3. $SiteURL = "siteurl"
  4. $LibraryName ="libraryname"
  5. $DownloadPath ="C:\temp"
  6. #Function to Download All Files from a SharePoint Folder
  7. Function Download-SPFolder($SPFolderURL, $DownloadPath)
  8. {
  9. Try {
  10. #Get the Source SharePoint Folder
  11. $SPFolder = $web.GetFolder($SPFolderURL)
  12. Write-host $SPFolder
  13. $DownloadPath = Join-Path $DownloadPath $SPFolder.Name
  14. #Ensure the destination local folder exists!
  15. If (!(Test-Path -path $DownloadPath))
  16. {
  17. #If it doesn't exists, Create
  18. $LocalFolder = New-Item $DownloadPath -type directory
  19. }
  20. #Loop through each file in the folder and download it to Destination
  21. ForEach ($File in $SPFolder.Files)
  22. {
  23. #Download the file
  24. $Data = $File.OpenBinary()
  25. $FilePath= Join-Path $DownloadPath $File.Name
  26. [System.IO.File]::WriteAllBytes($FilePath, $data)
  27. Write-host -f Green "`tDownloaded the File:"$File.ServerRelativeURL
  28. }
  29. #Process the Sub Folders & Recursively call the function
  30. ForEach ($SubFolder in $SPFolder.SubFolders)
  31. {
  32. If($SubFolder.Name -ne "Forms") #Leave "Forms" Folder
  33. {
  34. #Call the function Recursively
  35. Download-SPFolder $SubFolder $DownloadPath
  36. }
  37. }
  38. }
  39. Catch {
  40. Write-host -f Red "Error Downloading Document Library:" $_.Exception.Message
  41. }
  42. }
  43. #Main Function
  44. Function Download-SPDocumentLibrary($SiteURL, $LibraryName, $DownloadPath)
  45. {
  46. Try {
  47. #Get the Web
  48. $Web = Get-SPWeb $SiteURL
  49. #Delete any existing files and folders in the download location
  50. If (Test-Path $DownloadPath) {Get-ChildItem -Path $DownloadPath -Recurse| ForEach-object {Remove-item -Recurse -path $_.FullName }}
  51. #Get the document Library to Download
  52. $Library = $Web.Lists[$LibraryName]
  53. Write-host -f magenta "Downloading Document Library:" $Library.Title
  54. #Call the function to download the document library
  55. Download-SPFolder -SPFolderURL $Library.RootFolder.Url -DownloadPath $DownloadPath
  56. Write-host -f Green "*** Download Completed ***"
  57. }
  58. Catch {
  59. Write-host -f Red "Error Downloading Document Library:" $_.Exception.Message
  60. }
  61. }
  62. #Call the Function to export all document libraries from a site
  63. Download-SPDocumentLibrary $SiteURL $LibraryName $DownloadPath
Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment