Home » How to Get Folder Size in GB in PowerShell

How to Get Folder Size in GB in PowerShell

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.

The following method shows how you can do it with syntax.

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.

The following example shows how you can use this method.

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

Output:

PowerShell get folder size in GB
PowerShell get folder size in GB

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.

After running the PowerShell script for the $folderPath, it outputs folder size in GB = 1.04929186403751 GB

Conclusion

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

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.