Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Storage Analysis Report for SharePoint Document Library

$
0
0
Requirement: On a very large document library, We needed to analyze all documents stored, in-order-to move documents into individual libraries/folders, as this larger library causing latency issues. Lets use PowerShell script to analyze and generate report on document library storage information:

PowerShell script to generate storage metrics report on SharePoint library:
#Get Size of all Sub-sites in a Site Collection
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
#For MOSS 2007 compatibility
Function global:Get-SPWeb($url)
{
$site= New-Object Microsoft.SharePoint.SPSite($url)
if($site -ne $null)
{
$web=$site.OpenWeb()
}
return $web
}

# Function to Get Storage Metrics for a Library
Function GetStorageMetrics($Folder)
{
#Array to hold Storage data for all files
$StorageDataCollection = @()

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

#Create an object to hold storage data
$StorageDataResult = New-Object PSObject
#Set storage details to object property
$StorageDataResult | Add-Member -type NoteProperty -name "File Name" -value $File.Name
$StorageDataResult | Add-Member -type NoteProperty -name "File Size (KB)" -value ([Math]::Round(($FileSize/1KB),2))
$StorageDataResult | Add-Member -type NoteProperty -name "Versions Size (KB)" -value ([Math]::Round(($VersionSize/1KB),2))
$StorageDataResult | Add-Member -type NoteProperty -name "Total File Size (MB)" -value ([Math]::Round(($TotalFileSize/1MB),2))
$StorageDataResult | Add-Member -type NoteProperty -name "File Type" -value $File.Item['File Type']
$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

$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.
{
GetStorageMetrics($SubFolder)
}
}

#return [Math]::Round(($FolderSize/1MB),2)
return $StorageDataCollection
}

#Set Site URL and Library name variables
$WebURL = "http://sharepoint.crescent.com/finance/"
$LibraryName = "Invoices"

#Get the Web where library exists
$Web = Get-SPWeb $WebURL

#Get the Library's Root Folder
$Library = $Web.Lists[$LibraryName].RootFolder

#Call the function to get library's detailed storage Metrics
$StorageDetails = GetStorageMetrics($Library)

write-host "Total Number of Files in the library:" $Web.Lists[$LibraryName].ItemCount

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

#export the detailed storage Info the CSV file
$StorageDetails | sort-object "Total File Size (MB)" -descending | Export-csv "LibraryStroageRpt.csv" -notypeinformation
Write-host "Detailed Storage Report has been generated!"

$Web.dispose()

This script gives data back in CSV format,which we can open in Excel, add few more formatting to get the insights on where and what files occupying space and how we can categorize them.

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>