Get-PnPFolderItem is always returning "The attempted operation is prohibited because it exceeds the list view threshold."

john john 1,021 Reputation points
2022-08-30T11:22:25.24+00:00

I have the following script to get all root folders (we have 5.5 K) inside a document library:-

PS C:\WINDOWS\system32> $Folders=Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL  -ItemType Folder | Where {$_.Name -ne "Forms"}  

But it will raise this exception:-

Get-PnPFolderItem : The attempted operation is prohibited because it exceeds the list view threshold.
At line:1 char:10

  • $Folders=Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelative ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : WriteError: (:) [Get-PnPFolderItem], ServerException
  • FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.GetFolderItem

so how i can get the items in batches? as seems the Get-PnPFolderItem does not have a row limit or page size parameters?

Microsoft 365 and Office | SharePoint | For business | Windows
{count} vote

2 answers

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-08-31T06:36:26.577+00:00

    Hi @john john ,

    According to my research and testing, if you want to get items from larger lists with more than 5000 items, you should use PageSize to handle larger lists in batches ,please try to use the following PowerShell script:

    #Parameter  
    $SiteURL = "https://xxxx.sharepoint.com/sites/xxx"  
    $ListName= "xxx"  
        
    #Connect to PnP Online  
    Connect-PnPOnline -Url $SiteURL   
       
    #Get all list items from list in batches  
    $ListItems = Get-PnPFolderItem  -List $ListName -PageSize 500  
       
    Write-host "Total Number of List Items:" $($ListItems.Count)  
       
    #Loop through each Item  
    ForEach($Item in $ListItems)  
    {   
        Write-Host "Id :" $Item["ID"]  
        Write-Host "Title :" $Item["Title"]  
    }  
    

    More information for reference: SharePoint Online: Get List Items from Large Lists ( >5000 Items) using PowerShell without List View Threshold Exceeded Error

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. David Horsky 0 Reputation points
    2023-06-28T13:23:16.87+00:00

    DISCLAIMER: I have no solution for Get-PnPFolder Item, however, you can probably use Get-PnPListItem where using this query is possible and very effective. It'll effortlessly return all items in the List/DocumentLibrary.

    $getQuery = "<View Scope='RecursiveAll'><RowLimit>5000</RowLimit><OrderBy><$_.FieldValues.CS_DokumentyStavby.URl /></OrderBy></View>"

    $Items = Get-PnpListItem -List "ListName" -Query $getQuery

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.