Get Documents Inventory for a Site Collection using PowerShell

Requirement: Get all documents inventory in a CSV format from a SharePoint Site collection.

PowerShell to Get Documents Inventory of a SharePoint Site:

SharePoint Site collections can quickly become cluttered with files and documents. The document inventory can help keep track of what is stored in each site collection and where specific files are located. In this blog post, we will walk you through generating an inventory of all the documents in your SharePoint site using PowerShell and how to export the list to a CSV file.

sharepoint document inventory

Here is the PowerShell gets all document details from a given site:

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

$SiteURL = ""
$ReportFile =  "C:\Temp\SiteDocsInventoryRpt.csv"

# Function to retrieve Documents Inventory Report
Function Export-DocsInventory($List)
    #Array to hold Storage data for all files
    $DocInventoryCollection = @()
    #Query to batch process
    $Query = New-Object Microsoft.SharePoint.SPQuery
    $Query.ViewAttributes = "Scope='Recursive'"
    $Query.RowLimit = 2000
    $Counter = 1
    Do {
        #Get List Items defined by the Query
        $ListItems = $List.GetItems($Query)
        $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
        #Loop through each list Item                
        ForEach($Item in $ListItems)
            Write-Host "Processing File: $($File.Name) ($Counter of $($List.ItemCount))"
            #Get File Inventory
            $File = $Item.File
            $FileSize = $File.TotalLength
            $DocInventory = New-Object PSObject  
            $DocInventory | Add-Member -type NoteProperty -name "File Name" -value $File.Name
            $DocInventory | Add-Member -type NoteProperty -name "File Size(KB)" -value $($FileSize/1KB)
            $DocInventory | Add-Member -type NoteProperty -name "Created on" -value $File.TimeCreated 
            $DocInventory | Add-Member -type NoteProperty -name "Last Modified" -value $File.TimeLastModified 
            $DocInventory | Add-Member -type NoteProperty -name "Created by" -value $File.Author.Name        
            $DocInventory | Add-Member -type NoteProperty -name "Parent Folder" -value $File.ParentFolder.URL
            $DocInventory | Add-Member -type NoteProperty -name "URL" -value $File.URL 
            $DocInventoryCollection += $DocInventory
    } While ($Query.ListItemCollectionPosition -ne $Null)
    #Export the data to CSV File
    $DocInventoryCollection | Export-csv $ReportFile -notypeinformation -Append
#Get the site collection
$Site = Get-SPSite $SiteURL
$ResultData = @()
If(Test-Path $ReportFile) { Remove-Item $ReportFile }

#Ge All Sites of the Site collection
Foreach($web in $Site.AllWebs)
    Write-host -f Yellow "Processing Site: "$Web.URL
    #Get all Document Libraries - Exclude Hidden and System libraries
    $DocumentLibraries = $web.lists | Where-Object { ($_.hidden -eq $false) -and ($_.IsSiteAssetsLibrary -eq $false)  -and ($_.BaseType -eq "DocumentLibrary")}
    #Iterate through All lists and Libraries
    ForEach ($Library in $DocumentLibraries)
        #Call the function to get data 
        Export-DocsInventory $Library

In conclusion, getting a list of all documents in a SharePoint site collection is a useful way to manage and organize files within an organization. By using PowerShell, You can easily retrieve a list of all documents across multiple site collections and subsites. Also, you can filter and sort the results according to specific criteria by opening the generated CSV file in Microsoft Excel.

To generate a report on all documents in a SharePoint Online site collection, use: SharePoint Online: Get All Documents Inventory in a Site using PowerShell

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “Get Documents Inventory for a Site Collection using PowerShell

  • This is great! Is there a way to get the last accessed timestamp and possibly user for all files?

  • Thanks you for sharing your scripts. Unfortunately, this script is not working for me. I also do not see any errors after I change “Get-SPSite” to “Get-SPOSite” since I’m running this against SharePoint Online. I don’t see a connection established to the site collection. Can you help me figure out why this isn’t working for me?


Leave a Reply

Your email address will not be published. Required fields are marked *