Let's explore a cool and amazing way to extract the whole overall document library's nested folder structure, its respective documents and their respective file sizes in a CSV report using PnP PowerShell.
Here is the cool script for generating the above folder structure and related information in a fraction of a second:
- #Connect to SPO
- Connect - PnPOnline - Url "https://samplesharenet.sharepoint.com/sites/classictest"
- #My list target
- $myList = "/FlowCopyTestLib"
- #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"] / 1 MB).ToString("N")
- NbOfNestedFolders = $item["FolderChildCount"]
- NbOfFiles = $item["ItemChildCount"]
- }
- }
- }
- }
- #Export the results
- $results | Export - Csv - Path "D:\NestedFoldersForONEdoclib.csv" - NoTypeInformation
Just update your details in the above highlighted area and run the script on a Windows PowerShell ISE.
You will receive a cool CSV report generated with all the details in the mentioned path, say D:\ folder for my script as mentioned.
The CSV Report should contain all the below columns:LibraryName
- NbOfNestedFolders
- FolderSizeInMB
- NbOfFiles
- FullPath
- RootFolder
- FolderName
- FileType
You need to enter your log in details but make sure they are some Site Admin/Global Admin Details.
Note
The above script is only supported on SP Online Environment.
There is one more OOTB method by simply visiting the ink:- https://<TenantName>.sharepoint.com/sites/<SiteName>/_layouts/15/storman.aspx?root=<LibraryName> . Here you can also check individual files/documents size occupied which are not in any of the Nested Folders. This can be used further for your analysis.
Cheers!

Joe AyrePosted Mar 11, 2021, 10:43 PM
Handy little script - there are some typos though - many instances where the '-' symbol needs to have the spaces removed infront and behind it (especially for the parameters and commands), plus the @ { should be @{ and 1 MB should be 1MB.
Krish HegdePosted Jan 28, 2021, 9:25 AM
It is working fine, How can I extract all other file properties which we can add as a column in the folder or file view. Like "Modified By, created, Retention label, sensitivity"
Revathy AnimePosted Dec 4, 2020, 4:38 AM
It seems not working - please check for sharepoint online and kindly update the script. I tested and it seems not working :(