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:

This PowerShell gets all document details from a given site.

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

#Parameters
$SiteURL = "http://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

Information Technology Professional with Two decades of SharePoint Experience.

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

  • September 4, 2020 at 1:45 PM

    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