SharePoint Online: Get List Items from Folder using PowerShell

Requirement: Get all list items from a folder in SharePoint Online.

SharePoint Online: PowerShell to Get List Items in a Folder

Getting all items from the SharePoint list folder is a common requirement when working with client-side object model scripts. In this blog post, we will look at how you can use PowerShell to get list items from a particular folder. We will also be looking at exporting folder contents to a CSV file. This can be particularly useful if you need to quickly gather information from a specific folder in SharePoint Online. Let’s get started!

Here is the SharePoint CSOM to get folder items:

#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-ListItemsFromFolder()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [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 list
        $List=$Ctx.Web.Lists.GetByTitle($ListName)

        #Frame CamlQuery to retrieve the items from the Folder
        $CAMLQuery= New-Object Microsoft.SharePoint.Client.CamlQuery
        #Set relative URL of the folder
        $CAMLQuery.FolderServerRelativeUrl=$FolderURL   

        #Get List Items from the Folder
        $ListItems=$List.GetItems($CAMLQuery)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()

        Write-host "Total Number of Items Found:"$ListItems.Count

        #Iterate through all list items
        Foreach($Item in $ListItems)
        {
            #Get Ids for each Item
            Write-Host $item["ID"]
        }
    }
    Catch {
        write-host -f Red "Error Getting List Items from Folder!" $_.Exception.Message
    }
}

#Set Parameter Values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"

#Relative URL to the Folder - For Libraries, E.g: "/Documents/2018" or "/sites/sales/documents/projects/active"
$FolderURL="/Lists/Projects/Active"

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

This SharePoint Online PowerShell gets all items in the folder. If you want to get all files from a document library, use: PowerShell to get list of files in document library in SharePoint Online

To get all files and folders from a given folder, you must set the view scope. Otherwise, it gets all files and folders ONLY from the given folder location and not anything underneath!

$CAMLQuery.ViewXml = "<View Scope='RecursiveAll' /><Where><Eq><FieldRef Name='ServerRelativeUrl'/><Value Type='Text'>/Documents/Archived/2018</Value></Eq></Where>";

SharePoint Online: Get All Items in a Folder using PnP PowerShell

Here is the SharePoint CAML to get items in a folder:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Projects"
$FolderRelativeURL= "/Lists/Projects/2018"
 
#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
 
    #Get All Items from the Folder
    $CAMLQuery = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileDirRef'/><Value Type='Text'>$FolderRelativeURL</Value></Eq></Where></Query></View>"
    $FolderItems = Get-PnPListItem -List $ListName -Query $CAMLQuery
    
    Write-host "Total Number of Items in the Folder:" $FolderItems.Count
    ForEach($Item in $FolderItems)
    {
        Write-host $Item["Title"]
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

You can also use the Get-PnPFolderItem cmdlet with the URL of the folder to retrieve all items from a folder or sub-folder:

Get-PnPFolderItem -FolderSiteRelativeUrl "/Shared Documents/New"

Get All Files and Folders from a Folder Recursively

You may encounter a list view threshold exceeded issue when dealing with bigger lists and libraries, as Get-PnPFolderItem doesn’t have the “Pagesize” parameter. As a result, you may get a “Get-PnPFolderItem: The attempted operation is prohibited because it exceeds the list view threshold.” error. So, Here is the trick to get all items from a folder recursively:

#Set Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$FolderServerRelativeURL = "/Sites/Marketing/Shared Documents/New"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the Folder to download
$Folder = Get-PnPFolder -Url $FolderServerRelativeURL -Includes ListItemAllFields.ParentList
#Get the Parent List
$List = $Folder.ListItemAllFields.ParentList

#Get all Items from the folder - with progress bar
$global:counter = 0;
$ListItems = Get-PnPListItem -List $List -PageSize 500 -Fields FileLeafRef -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete `
                ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting Items from List:" -Status "Processing Items $global:Counter to $($List.ItemCount)";} | Where {$_.FieldValues.FileRef -like "$($FolderServerRelativeUrl)*"} 
Write-Progress -Activity "Completed Retrieving Items from Folder $FolderServerRelativeURL" -Completed

#Get All Files and Folders from the Folder Recursively
$ListItems | ForEach-Object {
    Write-host $_.FieldValues.FileRef
}

PowerShell to Get All Files and Folders Inventory from a Folder

How about generating an inventory report (CSV) of all files and folders from a given folder in SharePoint Online?

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Funds"
$ListName ="Funds"
$FolderServerRelativePath = "/sites/Funds/NeoFunds/EAEF IV*" #Any file under the given path   
$ReportOutput = "C:\Temp\EAEFIV-Inventory.csv"
$Pagesize = 500
#Array to store results
$Results = @()

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
$List  = Get-PnPList -Identity $ListName

$global:counter = 0; 
$ListItems = Get-PnPListItem -List $ListName -PageSize $Pagesize -Fields Author, Editor, Created, File_x0020_Type -ScriptBlock `
     { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `
        "Getting Documents from Library '$($List.Title)'" -Status "Getting Documents data $global:Counter of $($List.ItemCount)";} | Where {$_.FieldValues.FileRef -like $FolderServerRelativePath} 

$ItemCounter = 0
#Iterate through each item
Foreach ($Item in $ListItems)
{
        $Results += New-Object PSObject -Property ([ordered]@{
            Name              = $Item["FileLeafRef"]
            Type              = $Item.FileSystemObjectType
            FileType          = $Item["File_x0020_Type"]
            RelativeURL       = $Item["FileRef"]
            CreatedByEmail    = $Item["Author"].Email
            CreatedOn         = $Item["Created"]
            Modified          = $Item["Modified"]
            ModifiedByEmail   = $Item["Editor"].Email
        })
    $ItemCounter++
    Write-Progress -PercentComplete ($ItemCounter / ($ListItems.Count) * 100) -Activity "Exporting data from Documents $ItemCounter of $($ListItems.Count)" -Status "Exporting Data from Document '$($Item['FileLeafRef'])"        
}
  
#Export the results to CSV
$Results | Export-Csv -Path $ReportOutput -NoTypeInformation
   
Write-host "Folder Inventory Exported to CSV Successfully!" -f Green

This gets all items in a folder in SharePoint Online. Here is another post to get files from a folder SharePoint Online: Get All Files from Folder

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!

4 thoughts on “SharePoint Online: Get List Items from Folder using PowerShell

  • Hello,
    I want to create list items in Folders using by CSOM.
    Could you please give me tips or example to add list item in folder?
    Thanks,
    WonChul Choi

    Reply
  • Dear Salaudeen.
    Get All Files and Folders from a Folder Recursively works great.
    Thanks a lot for share your knowless
    Regards

    Reply
  • Dear Salaudeen,

    how to apply OCR on pdf inside document library (folder and subfolders) of sharepoint online ?

    Regards

    Reply

Leave a Reply

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