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
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
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
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
Here you go: Get Documents Inventory for a Site Collection using PowerShell
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.
Thanks so much. My mistake. should have see that. It work great now on MOSS 2007. Thanks again.
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
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]
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
The Line: $List = $Web.Lists($ListName) should be “$List = $Web.Lists[$ListName]”