Wednesday, 13 September 2017

Get web size using powershell

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

#Configure the location for the output file
$Output="D:\Scripts\SiteStorage.csv";

"Site URL" + "," + "Web URL" +  "," + "Primary Login" + "," + "Primary Email" + "," + "Secondary Login" + "," + "Secondary Email" + "," + "Root Site Last Modified" + "," + "Site Storage" | Out-File -Encoding Default -FilePath $Output

# 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 = Read-Host "Enter Site Collection URL"
$RootWeb = new-object Microsoft.SharePoint.SPSite($SiteURL)
$Webapp=$RootWeb.Webapplication

foreach ($Site in $Webapp.Sites)
{
  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)
       
$Site.Url + “,” + $Web.Url + “,” + $Site.Owner.UserLogin + “,” + $Site.Owner.Email + “,” + $Site.SecondaryContact.UserLogin + “,” + $Site.SecondaryContact.Email + “,” + $Web.LastItemModifiedDate.ToShortDateString() + “,” + $Size | Out-File -Encoding Default -Append -FilePath $Output

    #Dispose the object
    $web.dispose()
   }
}

No comments:

Post a Comment