Powershell script to Download Specific folders from Sharepoint

ZietZa 1 Reputation point
2022-03-26T03:42:12.27+00:00

Hi,

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

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,607 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Yi Lu_MSFT 17,456 Reputation points
    2022-03-30T09:31:17.893+00:00

    Hi @ZietZa
    You could try to use the following code:

    for(i=2022; i<2026; i++){  
    $FileRelativeURL1 = "/" + i + "/January/"+ i + "-January.xls"  
    $FileRelativeURL2 = "/" + i + "/February/"+ i + "-February.xls"  
    Download-FileFromLibrary -SourceFile $FileRelativeURL1  
    Download-FileFromLibrary -SourceFile $FileRelativeURL2  
    }  
      
      
      
    Function Download-FileFromLibrary()  
    {  
    param  
    (  
    [Parameter(Mandatory=$true)] [string] $SourceFile  
    )  
      
      
      
      
    $SiteURL = "https://Crescent.sharepoint.com/sites/marketing"  
    $SourceFile = "/sites/Marketing/Shared Documents" + $SourceFile  
    $DownloadPath ="C:\Temp"  
      
    #Get Credentials to connect  
    $Cred = Get-Credential  
      
    Try {  
    #Connect to PnP Online  
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred  
      
    #powershell download file from sharepoint online  
    Get-PnPFile -Url $SourceFile -Path $DownloadPath -AsFile  
    }  
    catch {  
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red  
    }  
      
      
      
    }  
    

    You could change the start year and end year according to your actual need.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Limitless Technology 39,351 Reputation points
    2022-04-01T13:33:48.303+00:00

    Hi @ZietZa

    This PowerShell script downloads all files and sub-folders from the given Folder in the SharePoint Online document library to a local directory.

    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 = " Site"
    $FolderSiteRelativeURL = "/Team Documents/2018"
    $DownloadPath ="C:\Docs"

    Connect to PnP Online

    Connect-PnPOnline -Url $SiteURL -Interactive

    Get the folder to download

    $Folder = Get-PnPFolder -Url $FolderSiteRelativeURL

    Call the function to download all files from a folder

    Download-SPOFolder $Folder $DownloadPath

    You can also try the script from this article https://learn.microsoft.com/en-us/answers/questions/306049/download-complete-folders-and-subfolders-in-sharep.html

    Hope this resolves your Query!!

    --
    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

  3. Carlos Ramos 0 Reputation points
    2023-07-21T22:53:38.25+00:00

    Thanks for your work @Yi Lu_MSFT

    I have made some improvements (exception management, work with long paths, progress bar):

    https://github.com/tuxramos/PowerShell/blob/main/downloadSharepointFolder.ps1

    0 comments No comments