≡ Menu

PowerShell script to query folder size

This little powershell function allows you to get the size of given folder. It has two parameters, -Name which takes file path as argument and -recurse which gets you size of the folders including the recursive items. For this powershell function -name is mandatory parameter and can take values from pipeline by property name.

function Get-FolderSize {
[CmdletBinding()]
param(
[parameter(valuefrompipelinebypropertyname=$true,mandatory=$true)]
$Name,
[switch]
$recurse            

)            

begin {}
process {
    if($recurse) {
        $colItems = (Get-ChildItem $name -recurse | Measure-Object -property length -sum)
        "Size of $name is -- " + "{0:N2}" -f ($colItems.sum / 1GB) + " GB"
    }
    else {
        $colItems = (Get-ChildItem $name | Measure-Object -property length -sum)
        "Size of $name is -- " + "{0:N2}" -f ($colItems.sum / 1GB) + " GB"
    }            

}
end {}
}

Output:

{ 1 comment… add one }
  • Almog July 23, 2013, 2:16 pm

    Hi ,
    Can you please to help me .
    I need script that will Do the job as Linux
    for example from Linux.
    Filesystem Size Used Avail Use% Mounted on
    /dev/hda2 15G 7.7G 5.8G 58% /
    /dev/hda1 996M 40M 905M 5% /boot
    tmpfs 1.8G 0 1.8G 0% /dev/shm

    thanks

Leave a Comment