Powershell: Get Folder Size ‘Quickly’

KB ID 0001660

Problem

Right clicking a folder and selecting properties is usually how you would see how large a folder is. Which is great, but if your folder size is HUGE (i.e. many terabytes) then this takes ages!

Solution

If you use PowerShell you can get the figure considerably quicker! Below I want to ge the size of E:\Shared;

[box]

In MegaBytes;
"{0:N2}" -f ((Get-ChildItem -path E:\Shared -recurse | Measure-Object -property length -sum ).sum /1MB) + " MB"

In GigaBytes;
"{0:N2}" -f ((Get-ChildItem -path E:\Shared -recurse | Measure-Object -property length -sum ).sum /1GB) + " GB"

In TeraBytes;
"{0:N2}" -f ((Get-ChildItem -path E:\Shared -recurse | Measure-Object -property length -sum ).sum /1TB) + " TB"

[/box]

You can even save this as a ‘function’ then call it whenever you wish.

[box]

function Get-FolderSize
{
 param([string]$pth)
 "{0:N2}" -f ((gci -path $pth -recurse | measure-object -property length -sum).sum /1GB) + " GB"
}

[/box]

 

Related Articles, References, Credits, or External Links

NA

4 thoughts on “Powershell: Get Folder Size ‘Quickly’

  1. In the screen shot you provide the parameter name in the function is hard coded to the value E:\Shared and not the variable name defined b ythe parameter.

  2. Hi! How long does the script usually take for folder size in TB, compared to selecting the folder properties? Does it have a significant difference? Thank you!

    • Yes – it can take a while if the target is sufficiently large – Apply a cup of coffee per TB at least 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *