SharePoint Online: Site Documents Inventory (Library, Folder, Sub-Folder and Files) Report using PowerShell

Requirement: Generate an Inventory report that contains all document Libraries, Folders, Sub-Folders, and File detail from a SharePoint Online site collection.

SharePoint Online: Site Inventory Report using PowerShell

Do you need to create an inventory report of all the documents in a SharePoint Online site? Well, You can use PowerShell to do that! In this blog post, I’ll show you how to create an inventory report of all the documents in a site using PowerShell script. As well as information about each document such as file name, file type, last modified date, etc.

We wanted to find the number of files, folders, sub-folders in each document library in a SharePoint Online site. This PowerShell script extracts the inventory of all document libraries, folders, sub-folders, and files from a given SharePoint Online site collection to a CSV report.

#Function to collect site Inventory
Function Get-PnPSiteInventory
   param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $Web)

    #Skip Apps
    If($Web.url -notlike "$SiteURL*") { return }
    Write-host "Getting Site Inventory from Site '$($Web.URL)'" -f Yellow
    #Exclude certain libraries
    $ExcludedLists = @("Form Templates", "Preservation Hold Library") 
    #Get All Document Libraries from the Web
    $Lists= Get-PnPProperty -ClientObject $Web -Property Lists
    $Lists | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $_.Title -notin $ExcludedLists -and $_.ItemCount -gt 0} -PipelineVariable List | ForEach-Object {
        #Get Items from List   
        $global:counter = 0;
        $ListItems = Get-PnPListItem -List $_ -PageSize $Pagesize -Fields Author, Created -ScriptBlock `
                 { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($_.ItemCount) * 100) -Activity "Getting Inventory from '$($_.Title)'" -Status "Processing Items $global:Counter to $($_.ItemCount)";}
        Write-Progress -Activity "Completed Retrieving Inventory from Library $($List.Title)" -Completed
            #Get Root folder of the List
            $Folder = Get-PnPProperty -ClientObject $_ -Property RootFolder
            $SiteInventory = @()
            #Iterate through each Item and collect data           
            ForEach($ListItem in $ListItems)
                #Collect item data
                $SiteInventory += New-Object PSObject -Property ([ordered]@{
                    SiteName  = $Web.Title
                    SiteURL  = $Web.URL
                    LibraryName = $List.Title
                    ParentFolderURL = $Folder.ServerRelativeURL
                    Name = $ListItem.FieldValues.FileLeafRef
                    Type = $ListItem.FileSystemObjectType
                    ItemRelativeURL = $ListItem.FieldValues.FileRef
                    CreatedBy = $ListItem.FieldValues.Author.Email
                    CreatedAt = $ListItem.FieldValues.Created
                    ModifiedBy = $ListItem.FieldValues.Editor.Email
                    ModifiedAt = $ListItem.FieldValues.Modified
            #Export the result to CSV file
            $SiteInventory | Export-CSV $CSVReport -NoTypeInformation -Append

$SiteURL = ""
$CSVReport = "C:\Temp\SiteInventory.csv"
$Pagesize = 2000

#Connect to Site collection
Connect-PnPOnline -Url $SiteURL -Interactive

#Delete the Output Report, if exists
If (Test-Path $CSVReport) { Remove-Item $CSVReport }    
#Call the Function for all Webs
Get-PnPSubWeb -Recurse -IncludeRootWeb | ForEach-Object { Get-PnPSiteInventory $_ }
Write-host "Site Inventory Report has been Exported to '$CSVReport'"  -f Green

Once executed, this script generates a CSV file in this format:

sharepoint online site inventory report using powershell

If you need to generate a documents inventory report for a one-single document library, use this script: SharePoint Online: PowerShell to List All Files Inventory in a Document Library

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

14 thoughts on “SharePoint Online: Site Documents Inventory (Library, Folder, Sub-Folder and Files) Report using PowerShell

  • Hello and thanks for this script!

    How do you run the report on a specific document library? For example, I want to run this script against a document library called “Shared With Guests”. I wasn’t quite sure where I can add that in.


  • Sorry, Connect-PnPOnline shows
    You are running the legacy version of PnP PowerShell.

    This version will be archived soon which means that while staying available, no updates or fixes will be released.
    Consider installing the newer prereleased cross-platform version of PnP PowerShell.
    This version has numerous improvements and many more cmdlets available.

    Maybe we have to update the code… Thanks a lot!

    • You can update to the new PnP PowerShell module with: Install-Module -Name “PnP.PowerShell”, However all the script in this article will work without any modification both in legacy version of PnP PowerShell and with the new module.

      • Good to know. Thanks!

  • Thanks for your excellent and helpful site!

    When I run this script, I get the following errors:

    WARNING: Parameter ‘Web’ is obsolete. The -Web parameter will be removed in a future release. Use Connect-PnPOnline -Url [subweburl] instead to connect to a subweb.
    Get-PnPListItem : Missing an argument for parameter ‘ScriptBlock’. Specify a parameter of type ‘System.Management.Automation.ScriptBlock’ and try again.
    At line:23 char:101
    + … -Web $web -PageSize $Pagesize -Fields Author, Created -ScriptBlock `
    + ~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-PnPListItem], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,PnP.PowerShell.Commands.Lists.GetListItem

  • This comment has been removed by the author.

  • This is fantastic, what is the syntax to add CONTENT TYPE as an additional field to export? Thank you for this and an excellent site

  • Too bad this is not listing the file size + number of versions 🙂

  • Love this… needed to find files modified for a SharePoint Online document library in the last week, and this script is ideal. The site inventory is beautifully presented in Excel, ideal for sorting & filtering etc. Thanks a million ��

  • Can I skip folders from this report? I just need files inventory. Please help

    • Sure, Just add a where condition to Filter: $ListItems | Where {$_.FileSystemObjectType -eq “File”}


Leave a Reply