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 a 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 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 $FolderSiteRelativeUrl -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 in an object array $FolderItems. 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 - RelativeURL
  
#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, We have used the Wildcard characters to the server Relative URL to include any files under the given path. Alternatively, you can use the script below to get all files from a specific folder in the SharePoint Online document library:

#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Retail/"
$ListName ="Documents"
$FolderServerRelativeURL = "/sites/Retail/Shared Documents/Classified"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
       
    #Get All Items from Folder in Batch
    $ListItems = Get-PnPListItem -List $ListName -PageSize 2000 | Sort-Object ID -Descending
      
    #Get List Items from the folder
    $ItemsFromFolder = $ListItems | Where {$_.FieldValues["FileDirRef"] -match $FolderServerRelativeURL }
    
    Write-host "Total Number of Items in the Library:"$ListItems.count
    Write-host "Total Number of Items in the Folder:"$ItemsFromFolder.count
    
    #Powershell to delete all files from a folder
    ForEach ($Item in $ItemsFromFolder)
    {
        #Remove-PnPListItem -List $ListName -Identity $Item.Id -Recycle -Force | Out-Null
        Write-host -f DarkYellow "Removed File at"$Item.FieldValues["FileRef"]
    }
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

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 SharePoint Online site. 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 to 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 - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

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

  • In the second script, the variable is named $FolderSiteRelativeUrl, but on line 13 $FolderURL is referenced. I think the variable should be named $FolderURL, instead. My script ran without errors after I made this change.

    Reply
    • Yes Jimmy! Fixed it. Thanks for the catch. Guess the variables are cached in PowerShell ISE from the first script.

      Reply
  • You defined the function as Get-FilesFromFolder(), but when you call it you used Get-ListItemsFromFolder.

    Reply
  • Doesn’t this script require visual studio? Where else do you get the csom binaries from?

    Reply

Leave a Reply

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