The 5,000 list item threshold in SharePoint Online is a performance limitation set by Microsoft. It's not actually a limit on the number of items you can store in a list or library; rather, it's a limit on the number of items that can be retrieved in a single query or view.
Using Powershell to count files in SharePoint libraries with over 5000 items:
#Requirements
Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force
Import-Module PnP.PowerShell
#Parameters
$SiteURL = "https://eias.sharepoint.com/sites/SITENAME"
$ListName = "02%20LIBRARYNAME"
$CSVFile = "C:\Temp\FolderStats.csv"
# Check if the folder exists, if not create it
$folderPath = Split-Path $CSVFile -Parent
if(!(Test-Path $folderPath)) {
New-Item -ItemType Directory -Force -Path $folderPath
}
#Connect to SharePoint Online
Connect-PnPOnline $SiteURL -Interactive
#Get the list
$List = Get-PnPList -Identity $ListName
#Get Folders from the Library - with progress bar
$global:counter = 0
$FolderItems = Get-PnPListItem -List $ListName -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 {$_.FileSystemObjectType -eq "Folder"}
Write-Progress -Activity "Completed Retrieving Folders from List $ListName" -Completed
$FolderStats = @()
#Get Files and Subfolders count on each folder in the library
ForEach($FolderItem in $FolderItems)
{
#Get Files and Folders of the Folder
Get-PnPProperty -ClientObject $FolderItem.Folder -Property Files, Folders | Out-Null
#Collect data
$Data = [PSCustomObject][ordered]@{
FolderName = $FolderItem.FieldValues.FileLeafRef
URL = $FolderItem.FieldValues.FileRef
FilesCount = $FolderItem.Folder.Files.Count
SubFolderCount = $FolderItem.Folder.Folders.Count
}
$Data
$FolderStats+= $Data
}
#Export the data to CSV
$FolderStats | Export-Csv -Path $CSVFile -NoTypeInformation
Using PowerShell for Detailed Stats:
#Requirements
PnP.PowerShell Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force
Import-Module PnP.PowerShell
#Parameters
$SiteURL = "https://SITESUBDOMAIN.sharepoint.com/sites/SITENAME"
$ListName = "LIBRARYNAME"
$CSVFile = "C:\Temp\FolderStats.csv"
# Check if the folder exists, if not create it
$folderPath = Split-Path $CSVFile -Parent
if(!(Test-Path $folderPath)) {
New-Item -ItemType Directory -Force -Path $folderPath
}
#Function to get number of Sub-folder and Files count recursively
Function Get-SPOFolderStats
{
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.Client.Folder]$Folder
)
#Get Sub-folders of the folder
Get-PnPProperty -ClientObject $Folder -Property ServerRelativeUrl, Folders | Out-Null
#Get the SiteRelativeUrl
$Web = Get-PnPWeb -Includes ServerRelativeUrl
$SiteRelativeUrl = $Folder.ServerRelativeUrl -replace "$($web.ServerRelativeUrl)", [string]::Empty
[PSCustomObject] @{
Folder = $Folder.Name
Path = $Folder.ServerRelativeUrl
ItemCount = Get-PnPFolderItem -FolderSiteRelativeUrl $SiteRelativeUrl -ItemType File | Measure-Object | Select -ExpandProperty Count
SubFolderCount = Get-PnPFolderItem -FolderSiteRelativeUrl $SiteRelativeUrl -ItemType Folder | Measure-Object | Select -ExpandProperty Count
}
#Process Sub-folders
ForEach($SubFolder in $Folder.Folders)
{
Get-SPOFolderStats -Folder $SubFolder
}
}
#Connect to SharePoint Online
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)
#Call the Function to Get the Library Statistics - Number of Files and Folders at each level
$FolderStats = Get-PnPList -Identity $ListName -Includes RootFolder | Select -ExpandProperty RootFolder | Get-SPOFolderStats | Sort Path
$FolderStats
#Export to CSV
$FolderStats | Export-Csv -Path $CSVFile -NoTypeInformation
For Libraries with Few Items:
- Go to the document library >> Choose "Edit Current View" from View menu.
- Tick "Folder Child Count” and "Item Child Count” columns >> Click OK.
The view now displays counts for each folder's top level.
Comments
0 comments
Please sign in to leave a comment.