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.
    return $StorageDataCollection

#Input Variables 
$WebURL = "" 
$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!"

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.