PowerShell script to get SharePoint 2013 library folders and subfolders size

Madhu Muppavarapu 1 Reputation point
2022-11-30T16:38:40.673+00:00

Could some share a PowerShell script to get SharePoint 2013 library folders and subfolders size, I have huge library which has hundreds of folders and each folder has a lot of subfolders and sub sub folders too.

Please help me to get a script for Site URL, Library, folder, subfolder, Size into a csv file.

Microsoft 365 and Office SharePoint Server For business
{count} votes

1 answer

Sort by: Most helpful
  1. Yanli Jiang - MSFT 31,596 Reputation points Microsoft External Staff
    2022-12-12T06:00:04.343+00:00

    Hi @Madhu Muppavarapu ,
    It took some time to deploy the environment and testing, sorry for the delay response.
    You can try the follow code.

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue  
    
    # Function to calculate folder size  
    Function CalculateFolderSize($Folder)  
    {  
        [long]$FolderSize0 = 0  
    
        foreach ($File in $Folder.Files)  
        {  
            #Get File Size  
            $FolderSize0 += $file.TotalLength;  
    
            #Get the Versions Size  
            foreach ($FileVersion in $File.Versions)  
            {  
                $FolderSize0 += $FileVersion.Size  
            }     
        }  
        #Iterate through all subfolders  
        foreach ($SubFolder in $Folder.SubFolders)  
        {     
            if($SubFolder.Name -ne "Forms") #Leave "Forms" Folder which has List default Aspx Pages.  
            {       
    
            #Call the function recursively  
            $FolderSize0 += CalculateFolderSize ($SubFolder)  
    
            }  
        }  
        Write-Host "Folder Name:" $Folder  
        Write-Host "Folder Size:" $FolderSize0 "B"  
        $resultsarray =@()   
            $fObject = new-object PSObject  
            $fObject | add-member -membertype NoteProperty -name "Name" -Value $Folder.Name  
            $fObject | add-member -membertype NoteProperty -name "Url" -Value $Folder.url  
            $fObject | add-member -membertype NoteProperty -name "Size" -Value $FolderSize0   
            $fObject | add-member -membertype NoteProperty -name "Unit" -Value "B"  
            $resultsarray += $fObject  
    
        $resultsarray| Export-csv c:\Users\spadmin\Desktop\0.csv -notypeinformation -Append  
        return $FolderSize0   
    
    }  
        $Web = Get-SPWeb "site url"  
    
        #Get the Library's Root Folder  
        $Folder =  $Web.Lists["libraryname"].RootFolder  
        $FolderSize=CalculateFolderSize($Folder)  
    

    The result:
    269600-12125.png
    269636-12126.png
    Sorry again.
    *
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.