Get SharePoint Documents Inventory Report with PowerShell

Requirement: Get all files in a SharePoint Document Library using PowerShell

This PowerShell script generates a detailed report on the given SharePoint library’s inventory in addition to its storage data. The output report contains:

  • File Name
  • File Size(KB)
  • Versions Size(KB)
  • Total File Size(KB)
  • Created on
  • Last Modified
  • Created by
  • Parent Folder
  • URL
SharePoint Document Library Inventory using PowerShell

SharePoint Document Library Inventory using PowerShell

Do you have a lot of documents stored in your SharePoint document libraries, And want to generate an inventory of all documents on your SharePoint site? PowerShell can help! This blog post will show you how to use PowerShell to create an inventory of all the documents in your SharePoint document libraries. This can be a great way to get a snapshot of the content that is stored in your SharePoint environment.

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

# Function to retrieve detailed Library storage Report
Function GetLibraryStorageRpt($Folder)
{
    #Array to hold Storage data for all files
    $StorageDataCollection = @()
    
    $FileSize = 0
    $TotalFileSize = 0
    foreach ($File in $Folder.Files)
    {
        #Get File Size
        $FileSize = $File.TotalLength
        
        $VersionSize = 0
        #Get the Versions Size
        foreach ($FileVersion in $File.Versions)
        {
            $VersionSize +=$FileVersion.Size
        }
        $TotalFileSize = $FileSize + $VersionSize
  
        $StorageDataResult = New-Object PSObject
 
        $StorageDataResult | Add-Member -type NoteProperty -name "File Name" -value $File.Name
        $StorageDataResult | Add-Member -type NoteProperty -name "File Size(KB)" -value $($FileSize/1KB)
        $StorageDataResult | Add-Member -type NoteProperty -name "Versions Size(KB)" -value $($VersionSize/1KB)
        $StorageDataResult | Add-Member -type NoteProperty -name "Total File Size(KB)" -value $($TotalFileSize/1KB)
        $StorageDataResult | Add-Member -type NoteProperty -name "Created on" -value $File.TimeCreated 
        $StorageDataResult | Add-Member -type NoteProperty -name "Last Modified" -value $File.TimeLastModified 
        $StorageDataResult | Add-Member -type NoteProperty -name "Created by" -value $File.Author.Name        
        $StorageDataResult | Add-Member -type NoteProperty -name "Parent Folder" -value $File.ParentFolder.URL
        $StorageDataResult | Add-Member -type NoteProperty -name "URL" -value $File.URL

        $StorageDataCollection += $StorageDataResult
        #Write-Host "Processing File:"$File.Name
    } 
        #Get Files in Sub Folders
        foreach ($SubFolder in $Folder.SubFolders)
        {   
         if($SubFolder.Name -ne "Forms") #Leave "Forms" Folder which has List default Aspx Pages.
             {
                GetLibraryStorageRpt($SubFolder)          
             }
        }   
    return $StorageDataCollection
}

#Input Variables 
$WebURL = "https://sharepoint.crescent.com/sites/operations" 
$ListName ="Documents"

#Get the List
$List = Get-SPWeb $WebURL | Select -ExpandProperty "Lists" | Where-Object{$_.Title -eq $ListName}

#Call the function to get data 
$StorageDetails = GetLibraryStorageRpt($List.RootFolder)

write-host "Total Number of Files:" $List.ItemCount 
write-host "Library Created by:" $List.Author

#Calculate the Total Size
$TotalSize = ($StorageDetails | Measure-Object 'Total File Size(KB)' -Sum | Select -expand Sum)  
Write-host "Library Size in MB: "([Math]::Round( ($TotalSize/1024),2))

#Export the data to CSV File
$StorageDetails | sort-object "Total File Size" -descending | Export-csv "$($ListName)_LibraryStroageRpt.csv" -notypeinformation
Write-Host "Storage Report has been Generated!"

If you want to generate an inventory report for all documents in a site collection, use: Get Documents Inventory for a Site Collection 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!

12 thoughts on “Get SharePoint Documents Inventory Report with PowerShell

  • Good morning Salaudeen,
    Fantastic content and a real help, i have amended very slightly to retrieve a few other snippets on info on the items but i am struggling with one and wondering if you would have a solution. i am trying to add a column to tell me if it has been declared as a “InPlace” record. i have tried various way including setting a variable $InPlace = Microsoft.Office.RecordsManagment.RecordsRepository.Records can calling that up. I have also tried variouse other cmdlets, but after two weeks of searching and trying i am at a loss, and advice would be really useful. And once again Thank you for your content

    Reply
    • You can use the cmdlet Test-PnPListItemIsRecord to check if a list item is declared as record! If you prefer CSOM, use; $ListItem.ListItemAllFields.Properties[“_vti_ItemDeclaredRecord”])

      Reply
      • Thank you for such a quick reply Salaudeen,

        I am still struggling to see how I can get it to return a result of identifying if its a record in the table. How would the powershell read?

        $StorageDataResult | Add-Member -type NoteProperty -name “records declared”

        Again thank you for you’re help with this as I am at a complete loss

        Kind regards

        Alex

        Reply
  • Hi Salaudeen

    Could you help me with a new requirement. I got the report for each of the file in a library but now how do I get a report of the size of each of the folders in a library?

    Thanks

    Reply
  • Hi Salaudeen

    Another question please. how do I use this script for the top site and all sub sites under it to call the Function to retrieve detailed Library storage Report for each site.

    Thanks

    Reply
  • Thanks so much. My mistake. should have see that. It work great now on MOSS 2007. Thanks again.

    Reply
  • Hi
    I tried to run this script on MOSS 2007 by adding
    [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) >null

    And changing this line but got the error. Could you help me resolve the error or i need to find some other property to use.

    $List = $WebURL | Select -ExpandProperty “Lists” | Where-Object{$_.Title -eq $ListName}
    Select-Object : Property “Lists” cannot be found.

    Thanks

    Reply
    • Hi Salaudeen
      sorry for being such a novice. here my PS

      [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) >null
      $url = “https://mosssp/sites/TestbySPAdmin”
      $ListName = “DocumentLibrary”
      Function global:Get-SPWeb($url)
      {
      $site= New-Object Microsoft.SharePoint.SPSite($url)
      if($site -ne $null)
      {
      $web=$site.OpenWeb();
      }
      return $web
      }
      $Web = Get-SPWeb($url)
      $List = $Web.Lists($ListName)

      but i got the error
      Method invocation failed because [Microsoft.SharePoint.SPWeb] doesn’t contain a method named ‘Lists’.
      At line:16 char:19

      Thanks

      Reply
    • The Line: $List = $Web.Lists($ListName) should be “$List = $Web.Lists[$ListName]”

      Reply

Leave a Reply

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