Get a Folder Size with PowerShell

Ad

It’s really easy to get the size of a folder with PowerShell. Even though the directory does not actually have a property for size, you just need to get the size of all of the files inside the folder.

function Get-FolderSize {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
$Path,
[ValidateSet("KB","MB","GB")]
$Units = "MB"
)
  if ( (Test-Path $Path) -and (Get-Item $Path).PSIsContainer ) {
    $Measure = Get-ChildItem $Path -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum
    $Sum = $Measure.Sum / "1$Units"
    [PSCustomObject]@{
      "Path" = $Path
      "Size($Units)" = $Sum
    }
  }
}

One of my favorite things about this script is the ability to choose to get the folder size in KB, MB or GB.

 

This won’t include any results from items that you don’t have read access to, so be aware of that. Otherwise, it’s a fast and easy way to get the folder size from PowerShell!

Ad
Ad
  • Skidrow Ray

    Great piece of code! kudos! +1