Get the number of folders and files from a document library

Frequent Contributor

Hi there, 

I am trying to get some information from a very big document library. Just under 600,000 files. 

I would like to restructure the content into several libraries but I am having a heck of a time understanding how it is structured. 

I tried running this PowerShell script, but I got an error as it exceeded the list view threshold. 

#Connect to SPO
Connect-PnPOnline -Url "https://<TENANT-NAME><YOUR-SITE>"

#My list target 
$myList = "/Shared Documents"
#Store the results
$results = @()
foreach ($list in $myList) {
    $allItems = Get-PnPListItem -List $list -Fields "FileLeafRef", "SMTotalFileStreamSize", "FileDirRef", "FolderChildCount", "ItemChildCount"
    foreach ($item in $allItems) {
        #Narrow down to folder type only
        if (($item.FileSystemObjectType) -eq "Folder") {
            $results += New-Object psobject -Property @{
                FileType          = $item.FileSystemObjectType 
                RootFolder        = $item["FileDirRef"]
                LibraryName       = $list 
                FolderName        = $item["FileLeafRef"]
                FullPath          = $item["FileRef"]
                FolderSizeInMB    = ($item["SMTotalFileStreamSize"] / 1MB).ToString("N")
                NbOfNestedFolders = $item["FolderChildCount"]
                NbOfFiles         = $item["ItemChildCount"]
#Export the results
$results | Export-Csv -Path "C:\Users\$env:USERNAME\Desktop\NestedFoldersForONEdoclib.csv" -NoTypeInformation

So I am kind of at a loss, then I found something called a file plan report from this link ... 

File Plan Report 

But I don't have that option ... does anyone else have any suggestions?


7 Replies

@Mike Dumka 
Do you need a CSV file with the details of all items or do you want to have a File Explorer like view which shows the total size, number of documents, number of folders within each folder?
Do you also need to get information on the document versions present, encrypted files and checked out items?

Paul | SLIM Applications

@Paul de Jong Just need how many items are in each folder. 

As strange as it may sound I don't even need to know the size of the files/folders. The reason I need the count, is because users are trying to sync the 600,000 files and there is a huge performance issue. The OneDrive sync client has a 300,000 per computer so that is why I need to spit this up. 


@Mike Dumka 

Splitting the library is indeed a good way forward to avoid using OneDrive to synchronise such a large data set. Having LVT problems only complicates things further. I am not sure how well PowerShell scripts can cope with the LVT. There must be other solutions out there but we have developed a solution that uses REST API and can cope with LVT limits. You can use the solution to browse through the structure or generate a detailed report (or csv list). You can use the trial version so there are no costs.

@Paul de Jong Thanks buddy! :)

I will check that out today!

@Paul de Jong 


Hey Paul, How are you ? Im facing the same issue here; LARGE DOCUMENT LIBRARY impacting on the Onedrive Sync; I need to create a plan to move/migrate to new document library but I need to check how many items have in each folder; Could you please help me with that ? Which product (TRIAL) should I use ?

Tks buddy!


Claudio Santini -