I am getting the below error:

The following exception occurred while trying to enumerate the collection: "Access is denied. 
(Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))".
At line:35 char:19
+   foreach($Web in $Site.AllWebs)
+                   ~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : ExceptionInGetEnumerator

When I run:

#Get Size of all Sub-sites in a Site Collection

# Function to calculate folder size
Function CalculateFolderSize($Folder)

$ErrorActionPreference = "SilentlyContinue" 
    [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 = "https://myurl.com.mt/"
$Site = new-object Microsoft.SharePoint.SPSite($SiteURL)

  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)
        Write-Host  $web.Url ":`t" $Size "MB"

    #Dispose the object

I am trying to get the size of each and every sub-site inside a site collection.

I have tried this same script on another site collection and it worked so it must not be something from the code.

Am I missing something from my end?

I am using SharePoint 2013 on-premise (farm)

  • It seems that to run the script, you need to have permissions to the SharePoint's content databases from where the size information is acquired. Have you verified this for the user you can running the script as? – moe Jan 4 '18 at 7:41

Solved this by adding myself as Farm Admin with Full Read/Full Control rights on the Web Application through Central Administration -> Web Application Management -> User Policy

  • 1
    Note that it's more recommended to run such administrative tasks with your service accounts, such as your farm administrator accounts. Administrative-wise it's not ideal to have actual users as your farm administrators. Anyway as this was the solution to your question, please mark it as an answer to the question. – moe Jan 5 '18 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.