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.
- # # # # # # # # # # # # # # # # # # # # Parameters # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
- $webUrl = "https://samplesharenet.sharepoint.com/sites/classictest";
- $listUrl = "PnPCopytoLib";
- $destination = "D:\\MegaArchival"
- # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
- Connect - PnPOnline - Url $webUrl
- $web = Get - PnPWeb
- $list = Get - PNPList - Identity $listUrl
-
- function ProcessFolder($folderUrl, $destinationFolder) {
- $folder = Get - PnPFolder - RelativeUrl $folderUrl
- $tempfiles = Get - PnPProperty - ClientObject $folder - Property Files
- if (!(Test - Path - path $destinationfolder)) {
- $dest = New - Item $destinationfolder - type directory
- }
- $total = $folder.Files.Count
- For($i = 0; $i - lt $total; $i++) {
- $file = $folder.Files[$i]
- Get - PnPFile - ServerRelativeUrl $file.ServerRelativeUrl - Path $destinationfolder - FileName $file.Name - AsFile
- }
- }
-
- function ProcessSubFolders($folders, $currentPath) {
- foreach($folder in $folders) {
- $tempurls = Get - PnPProperty - ClientObject $folder - Property ServerRelativeUrl
- #Avoid Forms folders
- if ($folder.Name - ne "Forms") {
- $targetFolder = $currentPath + "\"+ $folder.Name;
- ProcessFolder $folder.ServerRelativeUrl.Substring($web.ServerRelativeUrl.Length) $targetFolder
- $tempfolders = Get - PnPProperty - ClientObject $folder - Property Folders
- ProcessSubFolders $tempfolders $targetFolder
- }
- }
- }
- #Download root files
- ProcessFolder $listUrl $destination + "\"
- #Download files in folders
- $tempfolders = Get - PnPProperty - ClientObject $list.RootFolder - Property Folders
- 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!