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

-

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


Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap