0

I'm trying to get the exact size of SharePoint document libraries.

$listSize = 0
foreach ($item in $list.items) 
{ 
    $listSize += ($item.file).length
}

$listStorage = $listSize
Write-Host "Size of List : " $listStorage

But When I have actually compare size of Document library from SharePoint site settings --> Storage Metrics, it shows different.

Is the way I'm working is right. Please suggest some code.

  • 3
    It will certainly differ if you enable versioning. – Alexey Krasheninnikov May 18 '17 at 14:07
  • 1
    I think you just got three answers as a variation on your own solution. I would say Alexey has the correct answer. – Bunzab May 19 '17 at 7:54
3

Please find the below script, I used it to get the size.

Add-PSSnapin Microsoft.SharePoint.PowerShell 
$siteURL = "YOUR SITE URL HERE" 
$site = new-object Microsoft.SharePoint.SPSite($siteURL)
CLS
foreach ($web in $site.AllWebs)
{
    foreach ($list in $web.Lists)
    {
        if($list.BaseType -eq "DocumentLibrary")   
        {
            $listSize = 0
            foreach ($item in $list.items) 
            { 
                $listSize += ($item.file).length
            }
            Write-Host "Web Name: " -ForegroundColor Green -BackgroundColor Black -NoNewline
            Write-Host $web.Title -ForegroundColor Yellow -BackgroundColor Black 
            Write-Host "Library Name: " -ForegroundColor Green -BackgroundColor Black -NoNewline
            Write-Host $list.Title -ForegroundColor Yellow -BackgroundColor Black 
            Write-Host "Size: " -ForegroundColor Green -BackgroundColor Black -NoNewline
            Write-Host ([Math]::Round(($listSize/1KB),2)) "KB" -ForegroundColor Yellow -BackgroundColor Black 
            Write-Host "---------------------------------" -ForegroundColor Red -BackgroundColor Black
        }
    }
}
1
function GetFolderSizeInBytes ($Folder)
{
   [long]$folderSize = 0  
   foreach ($file in $Folder.Files)
   {
      $folderSize += $file.Length;
   }
   foreach ($fd in $Folder.SubFolders)
   {
      $folderSize += GetFolderSize -Folder $fd
   }
   return $folderSize
}
$sizeInBytes = GetFolderSizeInBytes($list.RootFolder)
$sizeInKb = $sizeInBytes/1024
$sizeInMb = $sizeInBytes/1024/1024
$sizeInGb = $sizeInBytes/1024/1024/1024

I hope this will help you

1

Can you please try with below script.

foreach ($web in $site.AllWebs)
{
  $SizeofDocLib=0  
  $count=0
  foreach ($list in $web.Lists)
    {

        if($list.BaseTemplate -eq "DocumentLibrary")   
            {
                 $listSize = 0
                 foreach ($item in $list.items) 
                    { 
                       $listSize += ($item.file).length
                    }
                 $listSize= [Math]::Round(($listSize/1MB),2)                                       
                 Write-Host $listSize "MB "
                 $SizeofDocLib+=$listSize
            }
     }
Write-Host Size :" "($SizeofDocLib/1024) "GB"
Write-Host $count
}

I used to get the size of document library with above.

1
function GetWebSizes ($StartWeb)
{
$web = Get-SPWeb $StartWeb
[long]$total = 0
$total += GetWebSize -Web $web
$total += GetSubWebSizes -Web $web
$totalInMb = ($total/1024)/1024
$totalInMb = "{0:N2}" -f $totalInMb
$totalInGb = (($total/1024)/1024)/1024
$totalInGb = "{0:N2}" -f $totalInGb
write-host "Total size of all sites below" $StartWeb "is" $total "Bytes,"
write-host "which is" $totalInMb "MB or" $totalInGb "GB"
$web.Dispose()
}

function GetWebSize ($Web)
{
[long]$subtotal = 0
foreach ($folder in $Web.Folders)
{
    $subtotal += GetFolderSize -Folder $folder
}
write-host "Site" $Web.Title "is" $subtotal "KB"
return $subtotal
}

function GetSubWebSizes ($Web)
{
[long]$subtotal = 0
foreach ($subweb in $Web.GetSubwebsForCurrentUser())
{
    [long]$webtotal = 0
    foreach ($folder in $subweb.Folders)
    {
        $webtotal += GetFolderSize -Folder $folder
    }
    write-host "Site" $subweb.Title "is" $webtotal "Bytes"
    $subtotal += $webtotal
    $subtotal += GetSubWebSizes -Web $subweb
}
return $subtotal
}

function GetFolderSize ($Folder)
{
[long]$folderSize = 0  
foreach ($file in $Folder.Files)
{
    $folderSize += $file.Length;
}
foreach ($fd in $Folder.SubFolders)
{
    $folderSize += GetFolderSize -Folder $fd
}
return $folderSize
}

Once you have run the script, you can call it with the following PowerShell command:

GetWebSizes -StartWeb <StartURL>

Using a start URL allows you to perform a size check from any site in the hierarchy – not just the top level site. In the example below, I ran it against the URL http://portal, but could also choose to run it from a sub-site, if required:

PS C:\> GetWebSizes -StartWeb http://portal
Site Intranet is 12763801 KB
Site Hello is 581759 Bytes
Site Team Site is 604175 Bytes
Total size of all sites below http://portal is 13949735 Bytes,
which is 13.30 MB or 0.01 GB

Reference:-

Get SharePoint Library Size with PowerShell and Powershell Script to find document library size where versions are enabled

0

I had this same question, with a few tweaks, but I had a problem most of the other answers here don't address - what if your document library has sub-folders?

I've written a script that will give you the number of actual files in a document library (that's not including folders) and the total size of everything in the library, recursively.

# Gets an object with NumFiles and TotalSize properties,
# which correspond with those of the list you pass in.
function Get-ListFileInfo($SPList)
{
    $name = $SPList.Title
    Write-Verbose "  $($name)"

    if($SPList.BaseType -eq "DocumentLibrary")
    {
        $fileInfo =  New-Object –TypeName PSObject –Prop (
            @{'NumFiles'=0;
            'TotalSize'=0}
        )

        $folder = $SPList.RootFolder

        $fileInfo = Get-DocLibFolderFileInfo $folder $true
        $fileInfo.NumFiles = $SPList.Items.Count
    }
    # You could expand this to other list types, too.
    # I left that out for brevity's sake.

    Write-Output $fileInfo
}

# Recursive function to get the filesize information from a folder
# Pass this the RootFolder of your SPList and it'll return the filesizes.
# used to count Files in here, too, but modified for accuracy
function Get-DocLibFolderFileInfo($SPFolder, [boolean] $recursive = $true)
{
    $filesInfo = New-Object –TypeName PSObject –Prop (
        @{'NumFiles'=$null;
        'TotalSize'=0})

    Write-Verbose "  Folder: $($SPFolder.Name)"

    if($SPFolder.Name -ne "Forms")
    {
        $files = $SPFolder.Files

        $filesHere = $files.Count

        Write-Verbose "    # Files: $($filesHere)"

        for($etr = 0;$etr -lt $filesHere; $etr++)
        {
            #$item = $files[$etr]
            $file = $files[$etr]
            #$file = ($item.file)
            if($file -ne $null)
            {
                if($file.Name.EndsWith(".aspx"))
                {
                    # skip views and the like, because we don't care.
                    Write-Verbose "    Skipping File: $($file.Name)";
                }
                else
                {
                    $fileSize = $file.Length
                    $filesInfo.TotalSize += $fileSize

                    Write-Verbose "    Filename: $($file.Name)"
                    Write-Verbose "      Size: $($fileSize)"
                }
            }
            else
            {
                Write-Warning "    Null file found?"
            }
        }
    }

    # Now recurse into subfolders
    if($SPFolder.SubFolders.Count -ne 0 -AND $recursive -eq $true)
    {
        $subFolders  = $SPFolder.SubFolders
        for($etr = 0;$etr -lt $subFolders.Count; $etr++)
        {
            $subFolder = $subFolders[$etr]
            $subFolderInfo = Get-DocLibFolderFileInfo $subFolder $true
            $filesInfo.TotalSize += $subFolderInfo.TotalSize;
        }
    }

    Write-Output $filesInfo
}

So to use these functions, just pass in your SPList object to Get-ListFileInfo and deal with the information it returns.

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.