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 Files detail from a SharePoint Online site collection.

SharePoint Online: Site Inventory Report using PowerShell

We wanted to find the number of files and 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
{
[cmdletbinding()]
   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
    Get-PnPList -Web $Web -PipelineVariable List | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $_.Title -notin $ExcludedLists -and $_.ItemCount -gt 0} | ForEach-Object {
        #Get Items from List   
        $global:counter = 0;
        $ListItems = Get-PnPListItem -List $_ -Web $web -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
        }
}

#Parameters
$SiteURL = "https://crescent.SharePoint.com/sites/Marketing"
$CSVReport = "C:\Temp\SiteInventory.csv"
$Pagesize = 2000

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

#Delete the Output Report, if exists
If (Test-Path $CSVReport) { Remove-Item $CSVReport }
    
#Call the Function for Rootweb and Subwebs
Get-PnPWeb | Get-PnPSiteInventory
Get-PnPSubWebs -Recurse| ForEach-Object { Get-PnPSiteInventory $_ }
Disconnect-PnPOnline
   
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

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

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

  • June 15, 2021 at 9:38 AM

    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

    Reply
  • April 26, 2021 at 4:25 PM

    This comment has been removed by the author.

    Reply
  • February 17, 2021 at 11:34 AM

    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

    Reply
  • October 2, 2020 at 12:04 PM

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

    Reply
  • July 20, 2020 at 2:25 PM

    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 ��

    Reply
  • June 19, 2020 at 8:47 PM

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

    Reply
    • June 19, 2020 at 8:50 PM

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

      Reply

Leave a Reply