PowerShell: How to Get Folder Size

To get the size of a folder using PowerShell we can make use of the combination of Measure-Object cmdlet from Microsoft.PowerShell.Utility module and Get-ChildItem command from the Microsoft.PowerShell.Management module.

Let's see the steps by breaking the one-liner first.


Step 1: We get all the child elements using Get-ChildItem

Get-ChildItem -Recurse -File -Force 'path/to/folder'

Step 2: Get the Size of Individual files using Measure-Object

Measure-Object -Property Length -Sum'

Step 3: Finally take a Sum of each file/object.

    Example:
    (Get-ChildItem -Recurse -File -Force 'data-folder' | Measure-Object -Property Length -Sum).Sum   

    32642393

PowerShell Get Size of a Folder

We can also make use of the gci and measure commands which are aliases to make the command shorter.

(gci 'data-folder' -r -fo | measure Length -Sum).Sum

References:

- https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.3

- https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/measure-object?view=powershell-7.3

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!