PowerShell: How to Get Folder Size in GB


You can use the following syntax in PowerShell to get the size (in GB) of each folder in a particular directory:

$path = "c:\users\bobbi\onedrive\documents\collecting_wisdom"
$folders = Get-ChildItem -Path $path -Directory -Recurse

$sizes = foreach ($folder in $folders) {
    $size = (Get-ChildItem -Path $folder.FullName -File -Recurse |
             Measure-Object -Property Length -Sum).Sum
    
    [PSCustomObject]@{
    Name = $folder.Name
    Size_GB = [Math]::Round($size / 1GB, 5)
    }
}

$sizes | Format-table

This particular example will display the name and size (in GB) of each folder in the directory that we specified in the $path variable.

Note: This particular example rounds the size of the file to 5 decimal places, but you can change the 5 to a different value in the Round() function if you would like to round to a different number instead.

The following example shows how to use this syntax in practice.

Example: How to Get Folder Size in GB Using PowerShell

Suppose that our current directory has the following path:

  • C:\Users\bobbi\OneDrive\Documents\collecting_wisdom

We can type Dir to list out all files in this current directory:

Suppose that we we would like to get the size of each folder in this directory in GB.

We can use the following syntax to do so:

$path = "c:\users\bobbi\onedrive\documents\collecting_wisdom"
$folders = Get-ChildItem -Path $path -Directory -Recurse

$sizes = foreach ($folder in $folders) {
    $size = (Get-ChildItem -Path $folder.FullName -File -Recurse |
             Measure-Object -Property Length -Sum).Sum
    
    [PSCustomObject]@{
    Name = $folder.Name
    Size_GB = [Math]::Round($size / 1GB, 5)
    }
}

$sizes | Format-table

The following screenshot shows how to use this syntax in practice:

PowerShell get folder size in GB

The output displays the name of each folder in the directory along with the folder size in GB:

  • Econometrics: 0.00235 GB
  • Environmental Stats: 0.00048 GB
  • Time Series Analysis: 0.00033 GB

Note: If you would like to display the folder size in MB instead, then simply divide by 1MB instead within the Round() method.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to List Files in Directory by Date
PowerShell: How to Get Most Recently Modified File in Directory
PowerShell: How to List All Files in Directory to Text File
PowerShell: How to Delete All Files with Specific Extension

Leave a Reply