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!