We have a SharePoint online document library which contain 40 folders on the root. now i want to count how many nested sub-folders and files each of the 40 folders contain? now in SharePoint the built-in "File Size" column will show how many direct folders and files exists and not the nested number of files and folders.

enter image description here

so can i write a power shell script or other approaches to get the total number of nested sub-folders and files?


2 Answers 2


You can use the SharePoint REST API to get the number of items with the property ItemCount

Sample of a REST:



  • you must expand Folder
  • you must select Folder/ItemCount

There are two methos to get count of sub-folders and files in each folder.

1.When a document library has multiple folders, sub-folder and files, you could get the number of sub-folders and files at each level by adding "Folder Child Count" and "Item Child Count" columns to the view.

Click Show or hide columns -> Select "Folder Child Count" and "Item Child Count" columns.

For example: A document library has a root folder testA, in the root folder testA has a sub folder 1 and test file, in the sub folder 1 has a sub folder 1-1.

enter image description here

2.Use PnP PowerShell to get count.

$SiteURL = "https://tenant.sharepoint.com/sites/emilytestnew"
$ListName = "0126"
$CSVFile = "C:\Folders.csv"
#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
    $FolderStats+= $Data
#Export the data to CSV
$FolderStats | Export-Csv -Path $CSVFile -NoTypeInformation


enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.