Get SharePoint Documents Inventory Report with PowerShell

This PowerShell script generates detailed report on the given SharePoint library's inventory in addition with 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
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 = "http://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

8 comments:

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

    ReplyDelete
    Replies
    1. Use this post to use PowerShell in MOSS 2007: How to use PowerShell with MOSS 2007?
      then:
      $Web = Get-SPWeb($url)
      $List = $Web.Lists[$ListName]

      Delete
    2. Hi Salaudeen
      sorry for being such a novice. here my PS

      [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") >null
      $url = "http://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



      Delete
    3. The Line: $List = $Web.Lists($ListName) should be "$List = $Web.Lists[$ListName]"

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

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

    ReplyDelete
    Replies
    1. Hi Salaudeen

      Thanks so much. That have everything I want to accomplish. But more than anything. Thanks for such clear explanation in each part of the script. Help me to understand and I learn a lot from this.

      Great work.

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.