I've got a script that successfully downloads all the content from a Sharepoint site but would like to change it so that it only downloads the content from certain folders with a specific name.
This is the script i'm using:
#Function to Download All Files from a SharePoint Online Folder - Recursively
Function Download-SPOFolder([Microsoft.SharePoint.Client.Folder]$Folder, $DestinationFolder)
{
#Get the Folder's Site Relative URL
$FolderURL = $Folder.ServerRelativeUrl.Substring($Folder.Context.Web.ServerRelativeUrl.Length)
$LocalFolder = $DestinationFolder + ($FolderURL -replace "/","\")
#Create Local Folder, if it doesn't exist
If (!(Test-Path -Path $LocalFolder)) {
New-Item -ItemType Directory -Path $LocalFolder | Out-Null
Write-host -f Yellow "Created a New Folder '$LocalFolder'"
}
#Get all Files from the folder
$FilesColl = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType File
#Iterate through each file and download
Foreach($File in $FilesColl)
{
Get-PnPFile -ServerRelativeUrl $File.ServerRelativeUrl -Path $LocalFolder -FileName $File.Name -AsFile -force
Write-host -f Green "`tDownloaded File from '$($File.ServerRelativeUrl)'"
}
#Get Subfolders of the Folder and call the function recursively
$SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType Folder
Foreach ($Folder in $SubFolders | Where {$_.Name -ne "Forms"})
{
Download-SPOFolder $Folder $DestinationFolder
}
}
#Set Parameters
$SiteURL = "https://zietza.sharepoint.com/sites/tm_Finance"
$LibraryURL = "/Shared Documents/Budgets/" #Site Relative URL
$DownloadPath ="C:\Reports\"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#Get The Root folder of the Library
$Folder = Get-PnPFolder -Url $LibraryURL
#Call the function to download the document library
Download-SPOFolder $Folder $DownloadPath
Here is my Sharepoint directory Structure:
/Shared Documents/Budgets/
--------------------------
/2022/January/2022-January.xls
/2022/February/2022-February.xls
up to December.
/2023/January/2023-January.xls
/2023/February/2023-February.xls
up to December.
/2024/
/2025/
etc
So i;d only like to download the content of the "January" Folder for each Year and end up with 2022-January.xls , 2023-January.xls , 2024-January.xls etc in my Download folder "C:\Reports"
I've searched for similar scripts and know it should look something like this:
Foreach ($Folder in $SubFolders | Where-Object { $_.Name -like "January*"})
but can;t get it to download the content of the folder.
Any help in the right direction is much appreciated.
Cheers, ZietZa
Here's how I would implement your requirements:
$LocalFolder = 'C:\Reports'
$UrlSharepoint = 'https://zietza.sharepoint.com'
$UrlSite = '/sites/tm_Finance'
$LibraryFolder = '/Shared Documents/Budgets'
# Connect to Sharepoint and save the connection
$Connection = Connect-PnPOnline -Url "${UrlSharepoint}${UrlSite}" `
-Credential $Credential `
-ReturnConnection `
-ErrorAction Stop
try {
Get-PnPFolderItem -FolderSiteRelativeUrl $LibraryFolder `
-Connection $Connection `
-ItemType File `
-Recursive |
Where-Object {$_.ServerRelativeUrl -like "*/January/*-January.xls" } |
Select-Object name, @{
name='SiteRelativePath'
expr={$_.ServerRelativeUrl.Substring($UrlSite.Length)}
} |
ForEach-Object {
Get-PnPFile -Url $_.SiteRelativePath `
-AsFile `
-Path $LocalFolder `
-Filename $_.name `
-Connection $Connection
}
}
finally {
# Make sure to disconnect
Disconnect-PnpOnline -Connection $Connection
}
Notes:
I used the -Recursive
parameter of Get-PnPFolderItem
to retrieve all files located in $LibraryFolder
and its subfolders (easier than having to recurse down folders yourself, and saves on back-and-forth communication between client and server)
Only files matching the wildcard */January/*-January.xls
are retained.
Files are retrieved and stored directly into the $LocalFolder
I connected with a predefined $Credential
, but you could use the -UseWebLogin
parameter instead if you need eg 2FA authentication. Just make sure to keep the -ReturnConnection
parameter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.