Skip to main content

How to Get Folder Size in GB in PowerShell

• 3 min read
powershell folder size gb measure-object

To get the size of a folder in gigabytes (GB) using PowerShell, you can use the Get-ChildItem cmdlet to list the items in the folder, and then calculate the total size of those items.

Method 1: Using Get-ChildItem with Measure-Object cmdlet

(Get-ChildItem -Path $folderPath -Recurse | Measure-Object -Property Length -Sum).Sum /1GB

This example will return the folder size in GB.

Get Folder Size in GB using the Get-ChildItem cmdlet in PowerShell

Use the Get-ChildItem cmdlet in PowerShell to retrieve the files and folders from the specified directory and then the Measure-Object cmdlet to compute the total size of these items.

# specify the folder path
$folderPath = "C:\temp\"

# compute the folder size in GB
$folderSizeinGB = (Get-ChildItem -Path $folderPath -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB

# output the size in gigabytes
Write-Output $folderSizeinGB

In this script, we define a variable $folderSize to store the folder size for which we want to compute the size in GB.

We then use the Get-Chidltem cmdlet with the -Recurse parameter to retrieve all files and folders from the specified $folderPath and pipe the output to the Measure-Object cmdlet.

The Measure-Object cmdlet calculates the sum of the Length property (file size in bytes) of all items in the folder. We then divide the total bytes by 1GB to get the total folder size in GB.

Finally, we use the Write-Output cmdlet, which outputs the folder size in GB.

Conclusion

I hope the above article on how to get folder size in GB using PowerShell is helpful to you.


File & Directory Operations

Disk & Storage Operations

  • PowerShell Get-Partition-Size - Partition size calculation
  • PowerShell Get-Partition - Get partition information
  • PowerShell Get-Disk - Get disk properties

Data Calculation & Aggregation

  • PowerShell Measure-Object - Calculate totals and statistics
  • PowerShell Group-Object - Group files by properties

Data Selection & Filtering

Date & Time Operations

Display & Formatting

Control Flow & Logic

Variables & Collections

Output & Export

File Management

Functions & Automation

  • PowerShell Functions - Create folder size functions
  • PowerShell Measure-Object - Aggregate file statistics

Comprehensive Guides