Monday, 29 May 2017

Get the SharePoint Site Size and write in Text using Powershell.

Get the SharePoint Site Size and write in Text using Powershell.

#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 = "Site url"
$Site = new-object Microsoft.SharePoint.SPSite($SiteURL)

$FilePath = '{0}\temp\scripts\pshell\dump.txt' -f $env:SystemDrive;

# Create a new custom object to hold our result.
$contactObject = new-object PSObject

  foreach($Web in $Site.AllWebs)
  {
    #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/1MB, 2)
    $webtitle =$web.Title
    $websurl=$web.Url
    #Export to txt file.
    $webtitle,$websurl,$Size -join '~' | Out-File -FilePath C:\temp\scripts\proddetails.txt -Append -Width 200;
    Write-Host  $web.Url "//`t"  $webtitle  ":`t" $Size "MB"


    #Dispose the object
    $web.dispose()
   }


Note: You can upload the size to html or even csv
for html try this. I had not tested it.
# Create object and store in array
$obj = New-Object System.Object
$obj | Add-Member -type NoteProperty -name "Description" -value "[Total Size]"
$obj | Add-Member -type NoteProperty -name "Size" -value "[$formatSize]"
$array += $obj

# Display
foreach($item in $array)
{
    write-output $item
}

# Write to HTML
$array | Select-Object | ConvertTo-Html -title "Site Size" | Set-Content .\SiteSize.html

No comments:

Post a Comment