SharePoint Online: Get All Files from a Folder using PowerShell

Requirement: SharePoint Online PowerShell to list files in a folder

PowerShell to Get Files from Folder in SharePoint Online:

Have you ever needed to get all the files from a folder in SharePoint Online? Maybe you are migrating files to a different location and need to grab everything from the folder. Perhaps, you want a quick way to get all the files from a given folder without navigating through each one. Whatever your reason, PowerShell can help! This blog post will show you how to use PowerShell to get all the files from a given folder in SharePoint Online.

From the web browser, use the “Export to Excel” button in the toolbar to get a list of all files and folders (from the subfolders recursively!).

powershell sharepoint online list files in folder

Using PowerShell, let’s retrieve all files from a given folder in the SharePoint Online document library.

#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 Get-FilesFromFolder()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $FolderURL
    )
    Try {
        #Setup Credentials to connect
        $Cred = Get-Credential
        $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred

        #Get the Folder and Files
        $Folder=$Ctx.Web.GetFolderByServerRelativeUrl($FolderURL)
        $Ctx.Load($Folder)
        $Ctx.Load($Folder.Files)
        $Ctx.ExecuteQuery()

        #Iterate through each File in the folder
        Foreach($File in $Folder.Files)
        {
            #Get Name for each File
            Write-Host $File.Name
        }
    }
    Catch {
        write-host -f Red "Error Getting Files from Folder!" $_.Exception.Message
    }
}

#Set Parameter Values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$FolderURL="/Branding/Classified/Finance Department Files"

#Call the function to get list items from folder
Get-FilesFromFolder -SiteURL $SiteURL -FolderURL $FolderURL

PnP PowerShell to Get All Files from a Folder in SharePoint Online:

To get all files from a folder recursively, we can use this PowerShell cmdlet Get-PnPFolderItem :

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$FolderSiteRelativeUrl = "/Shared Documents/2017" #Folder's Site Relative Path
 
#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
 
    #Get All Files from the Folder
    $FolderItems = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType File -Recursive
    
    Write-host "Total Number of Files in the Folder:" $FolderItems.Count
    ForEach($File in $FolderItems)
    {
        $File.Name
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This SharePoint Online PowerShell gets all files from the folder.

Although the above script works just fine for document libraries with <5000 items, on larger document libraries, the PnP PowerShell cmdlet Get-PnPFolderItem gives an error.

Get-PnPFolderItem : The attempted operation is prohibited because it exceeds the list view threshold.”.

So, to mitigate that issue, we can replace that with the Get-PnPListItem cmdlet.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
$ListName ="Branding"
$FolderServerRelativePath = "/sites/Marketing/Branding*" #Any file under the given path
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get all files from folder
Get-PnPListItem -List $ListName -PageSize 2000 | Where {$_.FieldValues.FileRef -like $FolderServerRelativePath -and $_.FileSystemObjectType -eq "File" } | ForEach-Object {
   Write-host $_.FieldValues.FileRef
}

Here is another PowerShell to get all items from a SharePoint Online folder: SharePoint Online: Get All Items in a Folder

How do I get a list of all SharePoint library files?

You can get a list of all SharePoint library files in a few different ways. One option is to use the “Export to CSV” feature from the toolbar, which will return a list of all SharePoint library files and their locations on your server. Another method is to use the PowerShell script to retrieve a list of all files and folders in a SharePoint Online document library.
More info: Get all files in SharePoint Online Document Library using PowerShell

How to download a document library from SharePoint Online?

The easiest way to download a document library is: Navigate to your SharePoint document library, select all Files and folders, and click on the “Download” button from the toolbar. This provides you with a Zip file with all files and folders from the document library.
You can also use: PowerShell to download a document library from SharePoint Online

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

5 thoughts on “SharePoint Online: Get All Files from a Folder using PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *