Home > PowerShell > PowerShell script to query folder size

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:

Categories: PowerShell Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>