none
How to get Sharepoint web size in powershell? RRS feed

  • Question

  • Is there any direct service(API or cmdlet) available to get the particular sharepoint online web site size. I tried using below code sample by iterating through all folders and files in web site, but it takes more time and returns the size that actually vary with original size mentioned in Storage metrics of the particular site.

    $username = "<office365username>"
    $password = Read-Host -Prompt "Enter password" -AsSecureString
    
    $siteUrl = “<sharepoint site URL>”
    
    $totalItemCount = 0
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password) 
    $ctx.Credentials = $credentials
    
    $rootWeb = $ctx.Web 
    $ctx.Load($rootWeb)
    $ctx.ExecuteQuery()
    $folders = $rootWeb.Folders
    $ctx.Load($folders)
    $ctx.ExecuteQuery()
    Write-Host $folders.Count
    
    function getItemSize($fold, $itemSize){
        [Long]$size = 0
        $ctx.Load($fold)
        $ctx.ExecuteQuery();
        $masterFolders = $fold.Folders;
        $ctx.Load($masterFolders);
        $masterFiles = $fold.Files;
        $ctx.Load($masterFiles);
        $ctx.ExecuteQuery();
        $size = $itemSize
        if($masterFiles.Count -gt 0){
             foreach($masterFile in $masterFiles){
                $ctx.Load($masterFile);
                $ctx.ExecuteQuery();
                $size += $masterFile.Length
                $ctx.Load($masterFile.Versions)
                if($masterFile.Versions.Count -gt 0){
                    foreach ($fileVersion in $masterFile.Versions)
                    {
                        $size += $fileVersion.Size
                    }
                }
            }
            $global:totalItemCount += $masterFiles.Count
        }
    
        if($masterFolders.Count -gt 0) { 
            foreach($folder in $masterFolders) {
                $masterFolderSize = getItemSize $folder $size
                $size =[int64]$masterFolderSize
            }
    
        }
        return $size
    
    }
    
    [long]$itemSize =0
    
    foreach($folder in $folders) {
        $folderSize = getItemSize $folder $itemSize
        $itemSize = [Int64]($folderSize)
    }
    Write-Host $itemSize "bytes"
    Write-Host $totalItemCount "Items"
    Write-Host "Success"; 

    Can anyone please point me in the right direction? Any help is also greatly appreciated.

    • Moved by Bill_Stewart Monday, February 27, 2017 3:46 PM Move to more appropriate forum
    Monday, February 27, 2017 2:33 PM

All replies