Powershell script to Download All Contents ( Folder and Files ) from Sharepoint and Local to SharePoint

Karl 20 Reputation points
2024-05-30T06:10:30.32+00:00

Hi There,

Good Day.

Would like to seek your assistance on how we able to transfer/download Folder with files and sub-folder, files to Local Drive from Sharepoint and Local to SharePoint.

Greatly appreciate your feedback. Thank You.

Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2024-05-31T02:54:32.3+00:00

    Hi @Karl,

    You could refer to following script to download file

    $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 "*" } | 
            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
    }
    
    
    

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.