none
Power shell script required for web application inventory site collection along with size RRS feed

  • Question

  • Hi all,

    I Need a poershell script to find out the number of site collections(sub sites as well) along with size.

    Please help to get the report on excel file.

    Regards,

    Abdulla khan

    Friday, January 17, 2020 9:14 AM

Answers

  • Hi Abdulla khan,

    For the site collections, it's easy to get the size. You could use the following script:

    Get-SPSite | Select @{ L = "URL"; E = { $_.url } }, @{ L = "size in MB"; E = { $_.usage.storage/1MB } } | Export-Csv -Path c:\siteCollectionSize.csv

    The results in the csv file would like this:

    For the report of subsites size, it's a bit complicated. You need to loop through all site collections and sub sites. You could try the below:

    $sites=get-spsite
    
    $array = @()
    foreach($site in $sites){
      foreach($web in $Site.AllWebs){
           $websize = GetFolderSize($web.RootFolder)
           foreach($recycleBinItem in $web.RecycleBin){
               $websize += $recycleBinItem.Size;
            }
           $size=$websize/1MB       
           # Create object and store in array
           $obj = New-Object System.Object
           $obj | Add-Member -type NoteProperty -name "url" -value $web.url
           $obj | Add-Member -type NoteProperty -name "Size in MB" -value $Size
           $obj | Add-Member -type NoteProperty -name "SiteCollection" -value $site.url
           $array += $obj 
      }
    }
    Function GetFolderSize($folder){
        $filesize = 0;
        foreach ($file in $folder.Files){
          $filesize += $file.TotalLength;
          foreach ($fileVersion in $file.Versions){
                $filesize += $fileVersion.Size;
            }
        }
      foreach ($subfolder in $folder.SubFolders){
            $filesize += GetFolderSize($subfolder)
        }
        return $filesize;
    }
    
    $array | Export-CSV "c:\subSiteSize.csv" -NoTypeInformation

    Best Regards,

    Michael Han


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Proposed as answer by taylor.l Friday, January 24, 2020 8:18 AM
    • Marked as answer by Abdullakhan Tuesday, February 4, 2020 11:51 AM
    Monday, January 20, 2020 7:33 AM
    Moderator
  • Hi Abdulla khan,

    Is there anything update?

    If my reply helps you, you could mark it as an answer.

    Best Regards,

    Michael Han


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Abdullakhan Monday, February 10, 2020 6:48 AM
    Friday, January 31, 2020 2:12 AM
    Moderator

All replies

  • Hi Abdulla khan,

    For the site collections, it's easy to get the size. You could use the following script:

    Get-SPSite | Select @{ L = "URL"; E = { $_.url } }, @{ L = "size in MB"; E = { $_.usage.storage/1MB } } | Export-Csv -Path c:\siteCollectionSize.csv

    The results in the csv file would like this:

    For the report of subsites size, it's a bit complicated. You need to loop through all site collections and sub sites. You could try the below:

    $sites=get-spsite
    
    $array = @()
    foreach($site in $sites){
      foreach($web in $Site.AllWebs){
           $websize = GetFolderSize($web.RootFolder)
           foreach($recycleBinItem in $web.RecycleBin){
               $websize += $recycleBinItem.Size;
            }
           $size=$websize/1MB       
           # Create object and store in array
           $obj = New-Object System.Object
           $obj | Add-Member -type NoteProperty -name "url" -value $web.url
           $obj | Add-Member -type NoteProperty -name "Size in MB" -value $Size
           $obj | Add-Member -type NoteProperty -name "SiteCollection" -value $site.url
           $array += $obj 
      }
    }
    Function GetFolderSize($folder){
        $filesize = 0;
        foreach ($file in $folder.Files){
          $filesize += $file.TotalLength;
          foreach ($fileVersion in $file.Versions){
                $filesize += $fileVersion.Size;
            }
        }
      foreach ($subfolder in $folder.SubFolders){
            $filesize += GetFolderSize($subfolder)
        }
        return $filesize;
    }
    
    $array | Export-CSV "c:\subSiteSize.csv" -NoTypeInformation

    Best Regards,

    Michael Han


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Proposed as answer by taylor.l Friday, January 24, 2020 8:18 AM
    • Marked as answer by Abdullakhan Tuesday, February 4, 2020 11:51 AM
    Monday, January 20, 2020 7:33 AM
    Moderator
  • Hi Abdulla khan,

    Is there anything update?

    If my reply helps you, you could mark it as an answer.

    Best Regards,

    Michael Han


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Abdullakhan Monday, February 10, 2020 6:48 AM
    Friday, January 31, 2020 2:12 AM
    Moderator