Featured image of post Using PowerShell to Calculate Folder Size from Command Line

Using PowerShell to Calculate Folder Size from Command Line

While the cmd command dir /s <folder> can also calculate size, it outputs too much unnecessary information. Filtering is possible, but using dir /s <folder> | find 'string' is not very universal. The English and Chinese outputs differ across Windows 7, 10, 11, and server versions, leading to poor compatibility. Therefore, I turn to PowerShell for a more convenient and concise solution!

You can use the cmd command dir /s <folder> to calculate folder size, but it generates too much irrelevant information. Filtering is possible, yet using dir /s <folder> | find 'string' isn’t very universal. The outputs in English and Chinese differ in Windows 7, 10, 11, and server versions, making compatibility quite poor. Hence, I turn to PowerShell for a simpler and more convenient solution!

Below is a PowerShell command that can be used to calculate the size of a folder and its subfolders:

Two-Line Command

1
2
$folderSize = Get-ChildItem -Path "<folder path>" -Recurse -File | Measure-Object -Property Length -Sum
$folderSize.Sum

Please replace <folder path> with the actual path of the folder you want to measure.

This command will recursively retrieve all files within the specified folder, calculate their total size, and output the total.

For example, to calculate the size of the folder C:\MyFolder and its subfolders, you can run the following command:

1
2
$folderSize = Get-ChildItem -Path "C:\MyFolder" -Recurse -File | Measure-Object -Property Length -Sum
$folderSize.Sum

After executing the command, you will see the total size of the folder C:\MyFolder and its subfolders.

You can also convert the output size to megabytes (MB). Here’s the corresponding PowerShell command:

1
2
3
$sizeInBytes = (Get-ChildItem -Path "<folder path>" -Recurse -File | Measure-Object -Property Length -Sum).Sum
$sizeInMB = $sizeInBytes / 1MB
"{0:N2} MB" -f $sizeInMB

Again, please replace <folder path> with the actual path of the folder.

This command first calculates the total size of all files in the specified folder and its subfolders. Then, it converts the size to megabytes (MB) and outputs the result.

For example, to display the size of the folder C:\MyFolder and its subfolders in MB, you can run:

1
2
3
$sizeInBytes = (Get-ChildItem -Path "C:\MyFolder" -Recurse -File | Measure-Object -Property Length -Sum).Sum
$sizeInMB = $sizeInBytes / 1MB
"{0:N2} MB" -f $sizeInMB

After executing the command, the output will look similar to:

123.45 MB

If you want to display the results in gigabytes (GB), simply replace all instances of MB with GB.

One-Line Command

You can condense the earlier PowerShell commands into a single line:

1
(Get-ChildItem -Path "<folder path>" -Recurse -File | Measure-Object -Property Length -Sum).Sum

Please replace <folder path> with the actual path of the folder you want to measure.

For example, to calculate the size of the folder C:\MyFolder and its subfolders in a single line, you can run:

1
(Get-ChildItem -Path "C:\MyFolder" -Recurse -File | Measure-Object -Property Length -Sum).Sum

Executing this command will display the total size of the folder C:\MyFolder and its subfolders.

Calculate Folder Size Using PowerShell from Command Line

Output in MB:

1
("{0:N2} MB" -f ((Get-ChildItem -Path "<folder path>" -Recurse -File | Measure-Object -Property Length -Sum).Sum / 1MB))

Please replace <folder path> with the actual path of the folder.

For example, to show the size of the folder C:\MyFolder and its subfolders in MB, you can run:

1
("{0:N2} MB" -f ((Get-ChildItem -Path "C:\MyFolder" -Recurse -File | Measure-Object -Property Length -Sum).Sum / 1MB))

Output in GB:

1
("{0:N2} GB" -f ((Get-ChildItem -Path "C:\MyFolder" -Recurse -File | Measure-Object -Property Length -Sum).Sum / 1GB))