Translate

Thursday, August 21, 2014

PS Script to do the sizing which includes all versions

#-----------------------------------
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


No comments:

Post a Comment