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.

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


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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap