Monday, September 26, 2016

Using PowerShell to find site/subsite/list/library size in SharePoint


Size of Site collection and all subsites


#Get Size of all Sub-sites in a Site Collection
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

# Function to calculate folder size
Function CalculateFolderSize($Folder)
{
    [long]$FolderSize = 0
 
    foreach ($File in $Folder.Files)
    {
   #Get File Size
        $FolderSize += $file.TotalLength;
   
  #Get the Versions Size
        foreach ($FileVersion in $File.Versions)
        {
            $FolderSize += $FileVersion.Size
        }   
    }
 #Iterate through all subfolders
    foreach ($SubFolder in $Folder.SubFolders)
    {
  #Call the function recursively
        $FolderSize += CalculateFolderSize $SubFolder
    }
    return $FolderSize
}

 
$SiteURL = "http://yoursite"
$Site = new-object Microsoft.SharePoint.SPSite($SiteURL)
  #Array to Hold Result - PSObjects
  $ListItemCollection = @()
 
  foreach($Web in $Site.AllWebs)
  {
    $ExportItem = New-Object PSObject
    #Call function to calculate Folder Size
    [long]$WebSize = CalculateFolderSize($Web.RootFolder)
   
    #Get Recycle Bin Size
    foreach($RecycleBinItem in $Web.RecycleBin)
        {
           $WebSize += $RecycleBinItem.Size
        }
        $Size = [Math]::Round(($WebSize/1024)/1024, 2)
        Write-Host  $web.Url ":`t" $Size "MB"
        $ExportItem | Add-Member -MemberType NoteProperty -name "Web URL" -value $web.Url
        $ExportItem | Add-Member -MemberType NoteProperty -name "Web Size" -value $Size
        $ListItemCollection += $ExportItem
    #Dispose the object
    $web.dispose()
   }
  
   $ListItemCollection | Export-CSV C:\allsubsitesof.csv -NoTypeInformation

Size of Lists and Libraries include version

#Set variables
$siteURL = "http://yoursite"
$site = new-object Microsoft.SharePoint.SPSite($siteURL)

$ListItemCollection = @()
foreach ($web in $site.AllWebs)
{
  foreach ($list in $web.Lists)
   {
   $ExportItem = New-Object PSObject
           $listSize = 0
           foreach ($item in $list.items)
            {
            $listSize += ($item.file).length
            foreach($FileVersion in $item.File.Versions) 
                {
                   $listSize += $FileVersion.Size
                }               
            }
            $Size = [Math]::Round(($listSize/1024)/1024, 2)
            Write-Host  $web.Title ":`t" $list.Title ":`t" $listSize   
            $ExportItem | Add-Member -MemberType NoteProperty -name "WebTitle" -value $web.Title
            $ExportItem | Add-Member -MemberType NoteProperty -name "WebURL" -value $web.URL
            $ExportItem | Add-Member -MemberType NoteProperty -name "WebDescription" -value $web.Description
            $ExportItem | Add-Member -MemberType NoteProperty -name "List_Library_Name" -value $list.Title
            $ExportItem | Add-Member -MemberType NoteProperty -name "Size (MB)" -value $Size
        $ListItemCollection += $ExportItem
    }
   
}
$ListItemCollection | Export-CSV C:\sizeoflistsandlibrary.csv -NoTypeInformation
$Site.Dispose()

0 comments:

Post a Comment