Download All Folders With Files from Doc Library onto File Share Local Path

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.   
  10. function ProcessFolder($folderUrl, $destinationFolder) {  
  11.     $folder = Get - PnPFolder - RelativeUrl $folderUrl  
  12.     $tempfiles = Get - PnPProperty - ClientObject $folder - Property Files  
  13.     if (!(Test - Path - path $destinationfolder)) {  
  14.         $dest = New - Item $destinationfolder - type directory  
  15.     }  
  16.     $total = $folder.Files.Count  
  17.     For($i = 0; $i - lt $total; $i++) {  
  18.         $file = $folder.Files[$i]  
  19.         Get - PnPFile - ServerRelativeUrl $file.ServerRelativeUrl - Path $destinationfolder - FileName $file.Name - AsFile  
  20.     }  
  21. }  
  22.   
  23. function ProcessSubFolders($folders, $currentPath) {  
  24.     foreach($folder in $folders) {  
  25.         $tempurls = Get - PnPProperty - ClientObject $folder - Property ServerRelativeUrl  
  26.         #Avoid Forms folders  
  27.         if ($folder.Name - ne "Forms") {  
  28.             $targetFolder = $currentPath + "\"+ $folder.Name;  
  29.             ProcessFolder $folder.ServerRelativeUrl.Substring($web.ServerRelativeUrl.Length) $targetFolder  
  30.             $tempfolders = Get - PnPProperty - ClientObject $folder - Property Folders  
  31.             ProcessSubFolders $tempfolders $targetFolder  
  32.         }  
  33.     }  
  34. }  
  35. #Download root files  
  36. ProcessFolder $listUrl $destination + "\"  
  37. #Download files in folders  
  38. $tempfolders = Get - PnPProperty - ClientObject $list.RootFolder - Property Folders  
  39. 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!