Click here to Skip to main content
15,880,972 members
Articles / Programming Languages / PowerShell
Tip/Trick

SharePoint 2010 - PowerShell Script to do the sizing - Includes all versions

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
22 Aug 2014CPOL2 min read 5.6K  
PowerShell script which allows to get the sizing of any URLs for SharePoint 2010

Introduction

SharePoint 2010 Content Inventory and Sizing tool 

Background

Complete content inventory for selected site collections or for any sub-sites. The content inventory is exported as comma separated text file which can be easily possible to transform to any other file providing information about all sites and individual level size including versioning. A complete and up-to-date Content Inventory provides the following benefits:

  • Reusable and scalable PowerShell script
  • Helps to improve the overall content quality by identifying stale and abandoned sites and lists and their size
  • Documents the site structure and allows to verify that the structure
  • Input for better planning of easy to complex content migration project
  • Reduce the cost of third purchasing third party tool
  • Extensively re-used for doing content inventory and sizing for migration and moving content from one environment to another 

The Content Inventory is PowerShell script so no installation is required.

Using the code

Use PowerShell to run the code - 

I have seen many questions and answers and scattered code and answer to get the sizing of site collection or at any level which can also includes versions and will output a text file which will be comma seperated.

You can convert this C:\outputsize.txt to excel or any file format for further analysis.

Find below the complete code and  used several times in my environment SharePoint 2010 farm environment.

#-----------------------------------
cls
if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin Microsoft.SharePoint.PowerShell
}

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\outputsize.txt -append

function GetWebSize ($Web)
{
[long]$subtotal = 0
foreach ($folder in $Web.Folders)
{
$subtotal += GetFolderSize -Folder $folder
}
$sitetotal=$subtotal
$SiteInGb = (($sitetotal/1024)/1024)/1024
$SiteInGb = "{0:N4}" -f $SiteInGb

write-host "," $Web.Url "," $SiteInGb "," 
write-host "`r`n"
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
}
$subsitetotal=$webtotal
$subsiteInGb = (($subsitetotal/1024)/1024)/1024
$subsiteInGb = "{0:N4}" -f $subsiteInGb

write-host "," $subweb.Url "," $subsiteInGb ","
write-host "`r`n"
$subtotal += $webtotal
$subtotal += GetSubWebSizes -Web $subweb
}
return $subtotal
}

function GetFolderSize ($Folder)
{
[long]$folderSize = 0 
foreach ($file in $Folder.Files)
{
if ($file.Versions -ne $NULL -and $file.Versions.Count -gt 1) {
foreach ($version in $file.Versions)
{
$folderSize += $version.Size;
}
}
else {
$folderSize += $file.Length;
}
}
foreach ($fd in $Folder.SubFolders)
{
$folderSize += GetFolderSize -Folder $fd
}
return $folderSize
}

$web = Get-SPWeb $args[0]
[long]$total = 0
$total += GetWebSize -Web $web
$total += GetSubWebSizes -Web $web

$totalInMb = ($total/1024)/1024
$totalInMb = "{0:N4}" -f $totalInMb
$totalInGb = (($total/1024)/1024)/1024
$totalInGb = "{0:N4}" -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()

Stop-Transcript
#--------------------------------------------

To run the above PowerShell - first copy the above code and save in a file with anyname.PS1 and fireup the PowerShell with administrator 

# how to run this script -

c:\>anyname.ps1 http://portal/

OR

c:\>anyname.ps1 http://portal/portal1

OR

c:\>anyname.ps1 http://portal/portal1/portal2

Points of Interest

Really time saving and very useful when your requirement is something to get all urls listing with size in GB.

History

You can get this information on internet but completly scattered and depending your requirement you can extend it to any level.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
SharePoint Architect
My interests - Community, Collaboration and Computer Technologies

Comments and Discussions

 
-- There are no messages in this forum --