Hello everyone,
Today I’ll share a little PowerShell script I made some time ago. It allows you to download all files of a single library, looping through all subfolders. This script use PNP PowerShell .
It was done for Office365 but should work the same way for onpremise. You just need to update the parameter on the top and run it. It will prompt you for your credentials to access the site and then download all the files to the specified directory (creating it if it doesn’t exist)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################### Parameters ########################################### | |
$webUrl = "https://YOURTENANTURL/sites/TestChristopher2"; | |
$listUrl = "Shared%20Documents"; | |
$destination = "C:\\Temp" | |
########################################################################### | |
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 + "\" |
Hoping this helps!
Christopher