SharePoint Online: Get All Files from a Folder using PowerShell

Requirement: SharePoint Online PowerShell to list files in folder.

PowerShell to Get Files from Folder in SharePoint Online:
Let's retrieve all files from a given folder in SharePoint Online document library using PowerShell.
#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()
        [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

        #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
$FolderURL="/Shared Documents/Documentation"

#Call the function to get list items from folder
Get-ListItemsFromFolder -SiteURL $SiteURL -FolderURL $FolderURL
This SharePoint online PowerShell gets all files from folder.

PnP PowerShell to Get All Files from a Folder in SharePoint Online:
To get all files from a folder, we can use:
#Config Variables
$SiteURL = ""
$ListName ="Branding"
$FolderServerRelativePath = "/sites/Marketing/Branding/2020*" #Any file under the given path
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Get all files from folder
Get-PnPListItem -List $ListName -PageSize 2000 | Where { $_.FieldValues.FileRef -like $FolderServerRelativePath} | ForEach-Object {
   Write-host $_.FieldValues.FileRef
Instead of getting all items and then applying filter, we can use Get-PnPFolderItem cmdlet to get all files from a sub-folder in SharePoint Online
#Config Variables
$SiteURL = ""
$FolderURL= "/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
    Write-host "Total Number of Files in the Folder:" $FolderItems.Count
    ForEach($File in $FolderItems)
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
Here is my another PowerShell to get all items from a SharePoint Online folder:SharePoint Online: Get All Items in a Folder


  1. Doesn't this script require visual studio? Where else do you get the csom binaries from?

    1. You don't need Visual studio. Just download and install CSOM SDK to get the assembly (.dll) files from

    2. Great tip. Thank you for your sharing


Please Login and comment to get your questions answered!

Powered by Blogger.