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

6 comments:

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

    ReplyDelete
    Replies
    1. Sure, Just add a where condition to Filter: $ListItems | Where {$_.FileSystemObjectType -eq "File"}

      Delete
  2. 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 ��

    ReplyDelete
  3. Too bad this is not listing the file size + number of versions :)

    ReplyDelete
  4. 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

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.