Get nested folders, files count, folder size, and more in SharePoint using PowerShell
Isn’t it nice to have some reports, inventory, or any visual about a SharePoint site? So we know what’s going on, at a high level at least.
Today’s post will be about running PnP PowerShell to extract info on a SharePoint site, and we’ll go fetch those nested folders! But that’s not all, we’ll also get the number of files in each folder, and the folder size!
Want another bonus? We’ll go through 1 document library, then through all document libraries in your site. Which means 2 PowerShell scripts! 🤠
Report on ONE particular document library
OK, let’s set the scene.
- One Site Collection
- One document library
- Multiple folders (with nested ones)
- Multiple files (maybe?)
- And we don’t know about the size…
So here we go.
So what are we doing here?
- First, as usual, we connect to our SharePoint site using the
- We target our document library to report on (
- And we create an array to store our results in (
Then we need to create 2x foreach (nested) loops:
- In the first one, we loop through each list item, declare the fields we are interested in, and we store the whole lot in a variable called
- In the second one, we narrow down by selecting only the “Folder” type using the
IFstatement, create a
New-Objectfor our values, and do a bit of customisation with the
SMTotalFileStreamSizeto format the output in MB.
Finally, we export the
$results into a
The export should look like this:
If we look at the first row, in the Shared Documents library, we have a folder called “Folder 1“, which has 3x nested folders & 1 file inside.
We can check this out in 2 ways: In SharePoint Online (you’ll have to trust me 😉)
But ALSO in the
csv file itself!
Look at the rows 4, 5, and 8? They contain the full path of those 3 sub-folders… So they were really here when I run the script.
Report on MULTIPLE document libraries
Now let’s have a look at how we can extend our scenario, and report on multiple document libraries within the site. This part will be shorter as the script will not change too much, so I’m not gonna bore you with a remake of the above
Only a couple of things have changed in this script:
- We use the
Get-PnPListto retrieve all the libraries, and store in a variable called
-Listparameter is now referring to
- And the
The rest is unchanged.
Output anyone? Sure!
Thanks for reading!