Calculate a Share Folder Size

Assuming your have access to a specific network share :

################################################
# Name: TreeSize
# Desc: Explores folder tree and gets total size.
# The Server list is stored on a text file.
# Date: 16/05/2011
# Auth: Alberto Ruiz
###############################################

#Check Parameters

Param($ServerPath)
cls
if($ServerPath -eq $Null) {
cls
write-host “You must especify the Folder Path”
write-host “Usage: TreeV.ps1 ”
}

$FolderSize =0
$FolderTree=get-childitem $ServerPath

Write-Host “Showing Content for $ServerPath”
Write-Host “———————————-”

$RootSize=0
Foreach ($FileName in $FolderTree) { $RootSize=$RootSize + $Filename.Length}
#Add the Root Folder Size in case there are some files.
$RootSize= $RootSize/1Gb
$RootSize = ”{0:N2}” -f $RootSize
Write-Host -ForeGroundColor Gray “+ $ServerPath ” -nonewline; Write-Host -foregroundColor White ” ($RootSize GB)”
#Rest of Folders
Foreach ($Folder in $FolderTree) {

if($Folder.mode -match “d”) {

get-childItem $Folder.fullname -recurse | foreach {$FolderSize +=$_.Length} #Size in Bytes

$FolderSize=$FolderSize/1Gb
$FSizeFormatted=”{0:N2}” -f $FolderSize
Write-Host -ForeGroundColor Gray “+ ” $Folder.name -nonewline; Write-Host -foregroundColor White ” ($fsizeFormatted GB)”

$TotalFolderSize=$TotalFolderSize + $FolderSize #To get the total folder size

}

}

$GlobalSum= ($TotalFolderSize)

#$filesize=(($FolderTree measure-object length -sum).sum)/1MB #

Write-Host “———————————-”
Write-Host -foregroundColor Yellow “Total Size : ” -nonewline; Write-Host -ForegroundColor Green ” $GlobalSum GB”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s