How to Get the Size of the Folder Including the Subfolders in PowerShell

Sheeraz Gul Feb 02, 2024
How to Get the Size of the Folder Including the Subfolders in PowerShell

This tutorial demonstrates how to get folder size, including subfolders using PowerShell.

Get the Size of the Folder, Including the Subfolders in PowerShell

To get the size of a folder, we need to run a few commands together. First, we need to get the children of the given folder, measure the length property for the folder, and finally, show the Sum object.

See the steps below:

The command to get the children of the given folder is:

Get-ChildItem "C:\Users"

The above command will show the children of the given folder. Here, the children mean the subfolders and other items:

    Directory: C:\Users


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         1/31/2022   1:24 PM                DefaultAppPool
d-r---         4/26/2021   3:32 PM                Public
d-----         11/1/2022   1:33 PM                Sheeraz
-a----         7/14/2022   2:33 PM            224 Example.java

As we can see, the folder has three subfolders and one file. Now, to get all the items of subfolders, we need to run the following command:

Get-ChildItem -Path "C:\Users" -Recurse -ErrorAction SilentlyContinue |

Then to measure the size of all subfolders and items, we need to run the following command:

Measure-Object -Property Length -Sum |

Finally, after measuring the length property for all the content of the folder, now show the object:

Select-Object Sum

The sum object will show the size of the folder, including subfolders. And to show the number of items in the folder, we use the Count object:

Select-Object Count

Run all the above commands together to show the output at once:

Get-ChildItem -Path "C:\Users" -Recurse -ErrorAction SilentlyContinue |
    Measure-Object -Property Length -Sum |
    Select-Object Sum
Select-Object Count

The output for the above commands is:

        Sum
        ---
38068067337

Count
-----
51885

The folder Users have the size 38068067337 bytes and 51885 number of items. We can also use a one-liner command to show the size of the given folder in MBS.

See the command:

"{0} MB" -f ((Get-ChildItem C:\Users\Sheeraz\ -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB)

The above command will show the size of folder Sheeraz in MBS. See the output:

36304.1059274673 MB
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PowerShell Folder