none
PowerShell + Get Size of all the Subsites RRS feed

  • Question

  • Hi All,

    I am using CSOM in PowerShell to get the size of site collection using Site.Usage.Storage ,is to possible to get the size of subsites?


    Please Mark it as answer if this reply helps you in resolving the issue,It will help other users facing similar problem

    Friday, June 15, 2018 11:11 AM

Answers

  • Hi,

    The following PowerShell for your reference:

    Function GetWebSize ($web)  
    {  
        $ctx.Load($web.Folders) 
        $ctx.ExecuteQuery() 
        [long]$total = 0  
        foreach ($folder in $web.Folders) {  
            $total += GetFolderSize -Folder $folder  
        }          
        return $total  
    }  
      
    Function GetFolderSize ($folder)  
    {  
        $ctx.Load($folder.Files) 
        $ctx.Load($folder.Folders) 
        $ctx.ExecuteQuery(); 
        [long]$folderSize = 0   
        foreach ($file in $folder.Files) { 
            $folderSize += $file.Length;  
        }  
        foreach ($fd in $folder.Folders) {  
            $folderSize += GetFolderSize -Folder $fd  
        }  
        return $folderSize  
    }  
     
    Function GetSubsiteSize ($web, $OutputFile) 
    { 
        $siteUrl = if($web -eq $null) { $siteCollectionUrl } else { $web.Url} 
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
        $ctx.Credentials = $credentials 
        $web = $ctx.Web  
        $ctx.Load($web)  
        $ctx.Load($web.Webs)     
        $ctx.ExecuteQuery() 
         
        Write-host $web.Url 
        $level = $($web.Url).Replace($siteCollectionUrl, "").Split("/").GetUpperBound(0);   
        $size = GetWebSize($web)  
        $sizeMB = "{0:N2}" -f (($size)/1MB)  
        $sizeGB = "{0:N2}" -f (($size)/1GB)  
        Write-Host $sizeGB $sizeMB 
        $record = "$($web.Id),`"$($web.Url)`",`"$($web.Title)`",$level,$($web.WebTemplate),$($web.LastItemModifiedDate),`"$sizeMB`",$sizeGB" 
        Add-Content $OutputFile $record 
     
        foreach($subweb in $web.Webs) {         
            GetSubsiteSize $subweb $OutputFile 
        } 
    } 
     
    Function GetAllWebsSize($OutputFile)  
    {  
        #Write CSV separated file header  
        Set-Content $OutputFile "WebId,Url,Name,Level,WebTemplate,LastItemModified,Size (MB),Size (GB)"     
     
        #pass $null for root site 
        GetSubsiteSize $null $OutputFile 
    } 

    More information is here:

    https://gallery.technet.microsoft.com/scriptcenter/SharePoint-Online-Size-of-04d578ad

    In SharePoint, we can use OOTB feature to get the storage of the site collection.

    http://sp2013/sites/team/_layouts/15/storman.aspx

    In SharePoint on-premise, we use Server Side Object Model to achieve it.

    https://www.c-sharpcorner.com/UploadFile/40e97e/calculating-size-of-site-collection-sub-sites-in-multiple-w/

    Best Regards,

    Dennis


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


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, June 18, 2018 3:11 AM
    Moderator

All replies

  • Hi,

    The following PowerShell for your reference:

    Function GetWebSize ($web)  
    {  
        $ctx.Load($web.Folders) 
        $ctx.ExecuteQuery() 
        [long]$total = 0  
        foreach ($folder in $web.Folders) {  
            $total += GetFolderSize -Folder $folder  
        }          
        return $total  
    }  
      
    Function GetFolderSize ($folder)  
    {  
        $ctx.Load($folder.Files) 
        $ctx.Load($folder.Folders) 
        $ctx.ExecuteQuery(); 
        [long]$folderSize = 0   
        foreach ($file in $folder.Files) { 
            $folderSize += $file.Length;  
        }  
        foreach ($fd in $folder.Folders) {  
            $folderSize += GetFolderSize -Folder $fd  
        }  
        return $folderSize  
    }  
     
    Function GetSubsiteSize ($web, $OutputFile) 
    { 
        $siteUrl = if($web -eq $null) { $siteCollectionUrl } else { $web.Url} 
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
        $ctx.Credentials = $credentials 
        $web = $ctx.Web  
        $ctx.Load($web)  
        $ctx.Load($web.Webs)     
        $ctx.ExecuteQuery() 
         
        Write-host $web.Url 
        $level = $($web.Url).Replace($siteCollectionUrl, "").Split("/").GetUpperBound(0);   
        $size = GetWebSize($web)  
        $sizeMB = "{0:N2}" -f (($size)/1MB)  
        $sizeGB = "{0:N2}" -f (($size)/1GB)  
        Write-Host $sizeGB $sizeMB 
        $record = "$($web.Id),`"$($web.Url)`",`"$($web.Title)`",$level,$($web.WebTemplate),$($web.LastItemModifiedDate),`"$sizeMB`",$sizeGB" 
        Add-Content $OutputFile $record 
     
        foreach($subweb in $web.Webs) {         
            GetSubsiteSize $subweb $OutputFile 
        } 
    } 
     
    Function GetAllWebsSize($OutputFile)  
    {  
        #Write CSV separated file header  
        Set-Content $OutputFile "WebId,Url,Name,Level,WebTemplate,LastItemModified,Size (MB),Size (GB)"     
     
        #pass $null for root site 
        GetSubsiteSize $null $OutputFile 
    } 

    More information is here:

    https://gallery.technet.microsoft.com/scriptcenter/SharePoint-Online-Size-of-04d578ad

    In SharePoint, we can use OOTB feature to get the storage of the site collection.

    http://sp2013/sites/team/_layouts/15/storman.aspx

    In SharePoint on-premise, we use Server Side Object Model to achieve it.

    https://www.c-sharpcorner.com/UploadFile/40e97e/calculating-size-of-site-collection-sub-sites-in-multiple-w/

    Best Regards,

    Dennis


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


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, June 18, 2018 3:11 AM
    Moderator
  • Hi,

    Would you please provide us with an update on the status of your issue?

    Best Regards,

    Dennis


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


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, June 26, 2018 9:37 AM
    Moderator
  • I have not tried running the script but I guess it would work hence marking it as answer

    Please Mark it as answer if this reply helps you in resolving the issue,It will help other users facing similar problem

    Wednesday, June 27, 2018 10:20 AM