Forum Discussion
dandraka
Apr 09, 2025Copper Contributor
Sharepoint Online: fastest way to retrieve folder count in a document library
Hi all
I'm really looking for what the title says. Right now I'm doing Get-PnPFolderItem -ItemType Folder (using Pnp.Powershell but can try other options) but with more than a few 10s of folders, this is REALLY slow.
The thing is, I just need a count (recursively), not all info as I'm getting with Get-PnPFolderItem.
Any ideas?
After trying many different things and getting a lot of "The attempted operation is prohibited because it exceeds the list view threshold" errors, I got... creative 🙂
I did the following:
# get file count from storage metrics $storageMetrics = Get-PnPFolderStorageMetric -FolderSiteRelativeUrl 'Shared Documents' -Connection $conn $fileCount = $storageMetrics | Select-Object -ExpandProperty TotalFileCount # now get total number of items in list $totalItemQuery = "/_api/web/lists/getbytitle('Documents')/ItemCount" $result = Invoke-PnPSPRestMethod -Url $totalItemQuery -Connection $conn # and subtract the files $folderCount = $result.value - $fileCount Write-Host "Total files $fileCount and folders $folderCount"
From my tests so far, this looks correct and REALLY fast. Can anyone think of a reason this might not be correct?
- dandrakaCopper Contributor
After trying many different things and getting a lot of "The attempted operation is prohibited because it exceeds the list view threshold" errors, I got... creative 🙂
I did the following:
# get file count from storage metrics $storageMetrics = Get-PnPFolderStorageMetric -FolderSiteRelativeUrl 'Shared Documents' -Connection $conn $fileCount = $storageMetrics | Select-Object -ExpandProperty TotalFileCount # now get total number of items in list $totalItemQuery = "/_api/web/lists/getbytitle('Documents')/ItemCount" $result = Invoke-PnPSPRestMethod -Url $totalItemQuery -Connection $conn # and subtract the files $folderCount = $result.value - $fileCount Write-Host "Total files $fileCount and folders $folderCount"
From my tests so far, this looks correct and REALLY fast. Can anyone think of a reason this might not be correct?