How To Get Folder Size Using PowerShell

PowerShell Folder SizePowerShell Folder Size

Checking an item’s or a folder’s size is pretty easy on all Windows computers. Just right-click the directory/item, click Properties, and you have the size in front of you. Correct?

Not everyone is as lucky as you. If the size of an item is significantly larger, say greater than 100 GB, then it can take a while for the Properties window to finally populate the size of the item. There surely has to be a quicker way to check the folder size.

You may need the size of a folder so you can estimate the time it will take to move it or to free up space on your hard drive. For whatever reason, you can instantly get the size of a folder/directory using Windows PowerShell.

This article shows you different cmdlets and how to use their switches to get the size of a folder on your PC almost instantly using PowerShell.

We have a separate guide on how to get folder sizes using File Explorer and third-party apps.

Commands to Get Folder Size in PowerShell

There are 2 primary commands that you can use in PowerShell to get the size of a folder. These commands can be used together with switches and parameters to filter the results further.

  • Command: Get-ChildItem

    Alias: gci

    An alias is an alternative name you can use for the command so you won’t have to type in the entire command every time.

    Details: This command grabs the information from one or specified directories, including the sub-directories. However, it does not display the empty directories.

  • Command: Measure-Object

    Alias: measure

    Details: This command is used to calculate the different properties of a specified directory, including its size.

These are the two primary cmdlets that we will be using today to get the size of a folder using PowerShell.

Get Folder Size Using PowerShell

Below are a few commands that you can use to get the size of a folder on your Windows PC. Note that this section only lists the cmdlets that will get you the size of the specified folder and the items inside only, and the results will not include the size of the items inside the sub-directories.

If you want to get the size of the items inside the sub-directories as well, refer to the next section below.

Let us now continue with the example of checking the size of the “D:\iTechtics\ISOs” folder we have on our PC. Note that you must replace the [PathToFolder] with the complete path to the folder/directory for which you want to get the size.

Run the following command to get the size of a folder in bytes:

Get-ChildItem [PathToFolder] | Measure-Object -Property Length -sum
Get folder size using PowerShell in Bytes
Get folder size using PowerShell in Bytes

The “Sum” field will display the size of the folder in Bytes, as in the image above. The approximate size of the items inside the “ISOs” folder is 83 GBs.

Note: The size in Bytes needs to be divided by 1024 to get the size in KiloBytes, then another 1024 for MegaBytes, and then another 1024 for GigaBytes. However, you do not need to perform this calculation since you can get the answer directly in MBs or GBs (discussed below).

To get the size of a folder in MBs or GBs, use the respective command below:

  • For MBs:

    (gci [PathToFolder] | measure Length -s).sum / 1Mb
    Get folder size using PowerShell in MBs
    Get folder size using PowerShell in MBs
  • For GBs:

    (gci [PathToFolder] | measure Length -s).sum / 1Gb
    Get folder size using PowerShell in GBs
    Get folder size using PowerShell in GBs

As you can see in the images above, the size in MBs and GBs is ambiguous to understand at a glance due to the size being in many decimal places. You can round off the result of the command using the following cmdlet while replacing “X” with the number of decimal places you want to round off your answer to.

Note: Replace “GB” and “MB” as needed.

"{0:NX} GB" -f ((gci [PathToFolder] | measure Length -s).sum / 1Gb)
Get folder size rounded off to decimal using PowerShell
Get folder size rounded off to decimal using PowerShell

Additionally, you can also get the size of all the items inside a folder having the same file type/extension. This command syntax can be used for this purpose where the file size of all items with the “.ISO” extension will be displayed:

(gci [PathToFolder] *.iso | measure Length -s).sum / 1Gb
Get size of specific file type items using PowerShell
Get size of specific file type items using PowerShell

Get Folder And Sub-Folder Sizes Using PowerShell

Up until now, all commands that we have discussed will show you the combined size of the individual items in the specified directory. If the parent directory has sub-directories, then the size of the items would not be accumulated in the results.

To get the size of the items inside the sub-folders as well, you must use the “-Recurse” parameter. This is used in conjecture with “-ErrorAction SilentlyContinue” to bypass directory access errors like “Permission is denied” etc.

Here are two examples of using the “-Recurse” parameter to get the size of the parent as well as the sub-directories:

(gci [PathToFolder] –Recurse -ErrorAction SilentlyContinue| measure Length -s).sum / 1Gb
"{0:NX} GB" -f ((gci –force [PathToFolder] –Recurse -ErrorAction SilentlyContinue| measure Length -s).sum / 1Gb)
Get folder and sub folder sizes using Recurse parameter in PowerShell
Get folder and sub-folder sizes using -Recurse parameter in PowerShell

Note that this cmdlet will also include the sizes of any hidden items.

On top of finding out the size of the parent and the child folders, you can also apply filters. For example, you can find the size of the items created on a specific day, a specific month, or a specific year. These are defined by putting in the starting and the ending dates, which are respectively defined by the “-gt” (greater than) and “-lt” (less than) parameters.

Here is the syntax for getting the size of a folder with sub-folders created in April 2022:

Note: The format of the dates is MM/DD/YY.

(gci -force [PathToFolder] –Recurse -ErrorAction SilentlyContinue | ? {$_.CreationTime -gt ‘4/1/22’ -AND $_.CreationTime -lt ‘4/30/22’}| measure Length -s).sum / 1Gb
Get folder and sub folder sizes with time filter using PowerShell
Get folder and sub-folder sizes with time filter using PowerShell

You can also apply these filters using the commands given earlier to get the folder sizes of the parent folders only.

Now, to make things more complicated, let’s assume that you must get the size of each sub-directory inside a folder. That can also be accomplished using PowerShell. Of course, the results will be inclusive of the size of the items inside the sub-directories and presented in a neat, tabular format.

That said, for this to work, you must either allow scripts to run inside PowerShell or use PowerShell ISE to run the following script:

$targetfolder='D:\'
$dataColl = @()
gci -force $targetfolder -ErrorAction SilentlyContinue | ? { $_ -is [io.directoryinfo] } | % {
$len = 0
gci -recurse -force $_.fullname -ErrorAction SilentlyContinue | % { $len += $_.length }
$foldername = $_.fullname
$foldersize= '{0:N2}' -f ($len / 1Gb)
$dataObject = New-Object PSObject
Add-Member -inputObject $dataObject -memberType NoteProperty -name “foldername” -value $foldername
Add-Member -inputObject $dataObject -memberType NoteProperty -name “foldersizeGb” -value $foldersize
$dataColl += $dataObject
}
$dataColl | Out-GridView -Title “Size of all subdirectories in D drive”
Get all subdirectory folder sizes using PowerShell script
Get all subdirectory folder sizes using PowerShell script

Of course, you can change the directory to scan as well as the name of the output file in the script given above.

Takeaway

No matter how big a file or a folder is, using PowerShell, you can instantly determine its size and plan ahead.

The methods shared in this post allow you to get the size of a folder, a folder as well as its subdirectories, the size of a specific file type items inside a folder, or even filter it during a specific time. Take your pick and find out the size of your folder(s).

These methods are especially useful if you wish to determine the size of a database before migration, site backups, or if you just want to clear out some space on the hard drive.

If you liked this post, Share it on:
Subhan Zafar is an established IT professional with interests in Windows and Server infrastructure testing and research, and is currently working with Itechtics as a research consultant. He has studied Electrical Engineering and is also certified by Huawei (HCNA & HCNP Routing and Switching).

1 comment

  • Guy Leech
    Guy Leech

    Useful but please be consistent – the units are MB and GB in the article but in the code you use Mb and Gb (which technically are different units megabits not megabytes, etc)

Leave your comment

Get Updates in Your Inbox

Sign up for the regular updates and be the first to know about the latest tech information

Talk to us now

Talk to us straight and get your questions answered right away

Tell Us About Your Project