Skip to main content

Download All Files From Library in SharePoint online

 #Load SharePoint CSOM Assemblies

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

 

Function Download-AllFilesFromLibrary()

{

    param

    (

        [Parameter(Mandatory=$true)] [string] $SiteURL,

        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,

        [Parameter(Mandatory=$true)] [string] $TargetFolder

    )

    Try {

         

        #Create Local Folder, if it doesn't exist

        $FolderName = ($SourceFolder.ServerRelativeURL) -replace "/","\"

        $LocalFolder = $TargetFolder + $FolderName

        If (!(Test-Path -Path $LocalFolder)) {

                New-Item -ItemType Directory -Path $LocalFolder | Out-Null

        }

         

        #Get all Files from the folder

        $FilesColl = $SourceFolder.Files

        $Ctx.Load($FilesColl)

        $Ctx.ExecuteQuery()

 

        #Iterate through each file and download

        Foreach($File in $FilesColl)

        {

            $TargetFile = $LocalFolder+"\"+$File.Name

            #Download the file

            $FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx,$File.ServerRelativeURL)

            $WriteStream = [System.IO.File]::Open($TargetFile,[System.IO.FileMode]::Create)

            $FileInfo.Stream.CopyTo($WriteStream)

            $WriteStream.Close()

            write-host -f Green "Downloaded File:"$TargetFile

        }

         

        #Process Sub Folders

        $SubFolders = $SourceFolder.Folders

        $Ctx.Load($SubFolders)

        $Ctx.ExecuteQuery()

        Foreach($Folder in $SubFolders)

        {

            If($Folder.Name -ne "Forms")

            {

                #Call the function recursively

                Download-AllFilesFromLibrary -SiteURL $SiteURL -SourceFolder $Folder -TargetFolder $TargetFolder

            }

        }

  }

    Catch {

        write-host -f Red "Error Downloading Files from Library!" $_.Exception.Message

    }

}

 

#Set parameter values

$SiteURL="<SITEURL>"

$LibraryName="Style Library"

$TargetFolder="D:\0\"

 

#Setup Credentials to connect

$Cred= Get-Credential

$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

 

#Setup the context

$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)

$Ctx.Credentials = $Credentials

      

#Get the Library

$List = $Ctx.Web.Lists.GetByTitle($LibraryName)

$Ctx.Load($List)

$Ctx.Load($List.RootFolder)

$Ctx.ExecuteQuery()

 

#Call the function: sharepoint online download multiple files powershell

Download-AllFilesFromLibrary -SiteURL $SiteURL -SourceFolder $List.RootFolder -TargetFolder $TargetFolder


Comments

Popular posts from this blog

Get App Expiry Dates using Powershell

Step 1: Connect-MsolService Step 2: $applist = Get-MsolServicePrincipal -all  |Where-Object -FilterScript { ($_.DisplayName -notlike "*Microsoft*") -and ($_.DisplayName -notlike "autohost*") -and  ($_.ServicePrincipalNames -notlike "*localhost*") } Step 3: foreach ($appentry in $applist) {     $principalId = $appentry.AppPrincipalId     $principalName = $appentry.DisplayName     Get-MsolServicePrincipalCredential -AppPrincipalId $principalId -ReturnKeyValues $false | ? { $_.Type -eq "Password" } | % { "$principalName;$principalId;" + $_.KeyId.ToString() +";" + $_.StartDate.ToString() + ";" + $_.EndDate.ToString() } | out-file -FilePath d:\appsec.txt -append }

Bulk Import Excel Data to SharePoint List Using PowerShell and PnP

  Managing large datasets in SharePoint can be tricky, especially when you're dealing with Excel files and need to avoid list view threshold issues. In this guide, I’ll walk you through a PowerShell script that efficiently imports data from Excel into a SharePoint Online list using PnP PowerShell — with batching support for performance. Prerequisites Make sure you have the following before running the script: SharePoint Online site URL Excel file with data properly formatted PnP PowerShell module installed ( Install-Module PnP.PowerShell ) Appropriate SharePoint permissions What the Script Does Connects to your SharePoint site Loads and reads an Excel file Converts Excel date values Batches records in groups (to avoid the 5000 item threshold) Adds the items to your SharePoint list or library Logs execution time PowerShell Script $siteUrl = "[Site Collection URL]" Connect-PnPOnline -Url $siteUrl -UseWebLogin # Capture the start time $startTime...

๐Ÿš€ Essential SPFx (SharePoint Framework) Commands for Every Developer

Whether you're new to SharePoint Framework (SPFx) or a seasoned pro, having a go-to list of core commands can significantly improve your development workflow. In this post, we'll walk through the most important SPFx CLI commands—from setting up your environment to packaging and deploying your solution. ๐Ÿ› ️ Setting Up Your SPFx Project Start by scaffolding a new project using the Yeoman generator provided by Microsoft. Make sure Node.js and npm are installed before you proceed. yo @microsoft/sharepoint After scaffolding the project, install all dependencies: npm install To update dependencies later: npm update ๐Ÿ” Trusting the Development Certificate If you're using the local workbench, you need to trust the developer certificate for HTTPS support: gulp trust-dev-cert ๐Ÿงช Running the Local Workbench To build and serve your project locally, use: gulp serve This command starts a local server at: https://localhost:4321/temp/workbench.html You can also test your solution in ShareP...