Introduction

Hi guys, let's explore a great way to download all files, folders, and nested folders available from an SP Online Document Library.
I am trying to create a Mega Archival Folder in my Local Path and download all the Files + Folder structure from an SP Online Doc Lib.
Open Windows PowerShell ISE and run the below script after adding your details in the highlighted areas.
  1. # # # # # # # # # # # # # # # # # # # # Parameters # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  2. $webUrl = "https://samplesharenet.sharepoint.com/sites/classictest";
  3. $listUrl = "PnPCopytoLib";
  4. $destination = "D:\\MegaArchival"
  5. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  6. Connect - PnPOnline - Url $webUrl
  7. $web = Get - PnPWeb
  8. $list = Get - PNPList - Identity $listUrl
  9. function ProcessFolder($folderUrl, $destinationFolder) {
  10. $folder = Get - PnPFolder - RelativeUrl $folderUrl
  11. $tempfiles = Get - PnPProperty - ClientObject $folder - Property Files
  12. if (!(Test - Path - path $destinationfolder)) {
  13. $dest = New - Item $destinationfolder - type directory
  14. }
  15. $total = $folder.Files.Count
  16. For($i = 0; $i - lt $total; $i++) {
  17. $file = $folder.Files[$i]
  18. Get - PnPFile - ServerRelativeUrl $file.ServerRelativeUrl - Path $destinationfolder - FileName $file.Name - AsFile
  19. }
  20. }
  21. function ProcessSubFolders($folders, $currentPath) {
  22. foreach($folder in $folders) {
  23. $tempurls = Get - PnPProperty - ClientObject $folder - Property ServerRelativeUrl
  24. #Avoid Forms folders
  25. if ($folder.Name - ne "Forms") {
  26. $targetFolder = $currentPath + "\"+ $folder.Name;
  27. ProcessFolder $folder.ServerRelativeUrl.Substring($web.ServerRelativeUrl.Length) $targetFolder
  28. $tempfolders = Get - PnPProperty - ClientObject $folder - Property Folders
  29. ProcessSubFolders $tempfolders $targetFolder
  30. }
  31. }
  32. }
  33. #Download root files
  34. ProcessFolder $listUrl $destination + "\"
  35. #Download files in folders
  36. $tempfolders = Get - PnPProperty - ClientObject $list.RootFolder - Property Folders
  37. ProcessSubFolders $tempfolders $destination + "\"
Speed Test Analysis: 10 Mb/2 Mins.
Creative Idea
You can use this Script to Download all the Files + Use script from my previous blog to apply the content on another Library. Integrate both the scripts on a Flow mechanism that can automatically trigger to run these scripts which can establish some Movement of Content for a greater purpose like Archival Purpose.
Cheers! Happy Modern SharePointing!