How to get the total number of nested files and folders inside the main folders inside a document library

john john Pter 100 Reputation points
2024-01-26T01:38:32.1166667+00:00

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. User's image

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

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,202 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,562 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Du-MSFT 39,531 Reputation points Microsoft Vendor
    2024-01-26T06:42:04.7166667+00:00

    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.

    1

    2.Use PnP PowerShell to get count.

    #Parameters
    $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
        }
        $Data
        $FolderStats+= $Data
    }
    #Export the data to CSV
    $FolderStats | Export-Csv -Path $CSVFile -NoTypeInformation
    

    Result:

    User's image


    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.