SharePoint Online: Get Files and Sub-Folders Count on Each Folder in a Document Library using PowerShell
Requirement: Get the count of files and sub-folders at each folder in a SharePoint Online document library.
How to Get the Number of Files and Sub-Folders at Each Folder of a Document Library?
When a document library has multiple folders, sub-folder and files, you can get the number of files and sub-folders at each level by adding "Folder Child Count" and "Item Child Count" fields to the view.
Now, the view shows the number of sub-folders and the number of items at the top-level of each folder.
Get Files, Sub-Folders Count at Each Folder in a Document Library using PowerShell
Here is the PowerShell to find the number of files and sub-folders at each folder in a SharePoint Online document library using PnP PowerShell.
How to Get the Number of Files and Sub-Folders at Each Folder of a Document Library?
When a document library has multiple folders, sub-folder and files, you can get the number of files and sub-folders at each level by adding "Folder Child Count" and "Item Child Count" fields to the view.
- Navigate to the document library >> Click on View drop-down menu >> "Edit Current View"
- Select "Folder Child Count" and "Item Child Count" columns and Click on OK.
Now, the view shows the number of sub-folders and the number of items at the top-level of each folder.
Get Files, Sub-Folders Count at Each Folder in a Document Library using PowerShell
Here is the PowerShell to find the number of files and sub-folders at each folder in a SharePoint Online document library using PnP PowerShell.
#Parameters $SiteURL = "https://crescent.sharepoint.com/sites/marketing" $ListName = "Project Documents" #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)", "" [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 "C:\Temp\DocLibStats.csv" -NoTypeInformationThis script outputs the data on screen and also generates a CSV file!
I cannot get this script to report sub-folders Past the top level. Something might be broken with the latest PnP module. I get the following output.
ReplyDeleteFolder Path ItemCount SubFolderCount
------ ---- --------- --------------
Test Library /Test Library 0 2
Forms /Test Library/Forms 0 0
Document /Test Library/Forms/Document 0 0
Level 1 /Test Library/Level 1 0 0
Level 2-1 /Test Library/Level 1/Level 2-1 0 0
Level 3-1 /Test Library/Level 1/Level 2-1/Level 3-1 0 0
Level 2-2 /Test Library/Level 1/Level 2-2 0 0