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. A 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

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$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
            $Counter++
        }
    } 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
    }
}

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

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

  • 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?

    Reply

Leave a Reply