How to Get Size of Folder in PowerShell | Get folder size in GB in PowerShell

Recently, I got a requirement to get the size of the folder in PowerShell. In this blog post, we’ll explore different methods or cmdlets to get the size of a folder using PowerShell, complete with examples and scripts.

To get the size of a folder using PowerShell, you can use the Get-ChildItem cmdlet in combination with the Measure-Object cmdlet. For example, $folderSize = (Get-ChildItem -Path “C:\ExampleFolder” -Recurse -File | Measure-Object -Property Length -Sum).Sum will give you the size of the folder in bytes.

How to Get the Size of Folder in PowerShell

There are multiple ways to get the size of the folder in PowerShell. Let us discuss each and every method with examples.

1. Using Get-ChildItem and Measure-Object

One of the easiest methods to calculate the size of a folder in PowerShell is by using the Get-ChildItem cmdlet combined with Measure-Object. The Get-ChildItem cmdlet retrieves the files and folders in a specified directory, and Measure-Object calculates the properties of these objects, such as the sum of their sizes.

Here’s a basic example of how to use these cmdlets to get the size of a folder in PowerShell:

$folderPath = "C:\MyFolder"
$folderSize = (Get-ChildItem -Path $folderPath -Recurse -File | Measure-Object -Property Length -Sum).Sum
$folderSizeInMB = [math]::Round($folderSize / 1MB, 2)
Write-Host "The size of $folderPath is $folderSizeInMB MB"

In this script, we specify the folder path and use the -Recurse flag to include all subfolders and files. The -File switch ensures that only files are measured, not subdirectories. The Measure-Object cmdlet is then pipelined to sum up the Length property of all files, which represents their size in bytes. Finally, we convert the size to megabytes for easier reading.

Check out the screenshot below for the output after I executed the PowerShell script using VS code.

Get the Size of Folder in PowerShell

2. Using COM Objects for Faster Performance

For larger directories, you might notice that the Get-ChildItem method can be slow. To speed up the process, you can use a COM object, such as Scripting.FileSystemObject, which provides a faster way to get folder sizes.

Here’s how you can use a COM object to get the size of a folder in PowerShell:

$folderPath = "C:\MyFolder"
$fsObject = New-Object -ComObject Scripting.FileSystemObject
$folder = $fsObject.GetFolder($folderPath)
$folderSize = $folder.Size
$folderSizeInGB = [math]::Round($folderSize / 1GB, 2)
Write-Host "The size of $folderPath is $folderSizeInGB GB"

This script creates a new COM object for file system operations and retrieves the size of the specified folder. The size is then converted to gigabytes for a more convenient measure.

3. Advanced Scripting with Robocopy

Another advanced method to get folder sizes is by using robocopy.exe. Robocopy is a robust file copy command-line tool included in Windows, but it can also be used to calculate folder sizes by enabling its logging capabilities and then parsing the output.

Here’s an example of using Robocopy to get folder size:

$folderPath = "C:\ExampleFolder"
$tempFile = "C:\temp\robocopy_log.txt"
robocopy $folderPath $folderPath /L /XJ /E /NFL /NDL /NJH /NJS /nc /ns /np > $tempFile
$folderSize = (Select-String "Bytes :.*" $tempFile -AllMatches | ForEach-Object { $_.Matches } | ForEach-Object { $_.Value.Split(":")[1].Trim() } | Measure-Object -Sum).Sum
$folderSizeInTB = [math]::Round($folderSize / 1TB, 4)
Write-Host "The size of $folderPath is $folderSizeInTB TB"

This script uses robocopy to generate a log file without actually copying any files (/L switch). It then parses the log to find the total bytes and converts the size to terabytes.

Displaying Folder Sizes in a Human-Readable Format

When presenting folder sizes to users, it’s often helpful to format the sizes in a human-readable way, such as KB, MB, GB, etc. Here’s the PowerShell script that gets the folder size and converts it to a more readable format:

function Format-FileSize {
    Param ([int64]$size)
    If ($size -gt 1TB) {
        [math]::Round($size / 1TB, 2).ToString() + " TB"
    } ElseIf ($size -gt 1GB) {
        [math]::Round($size / 1GB, 2).ToString() + " GB"
    } ElseIf ($size -gt 1MB) {
        [math]::Round($size / 1MB, 2).ToString() + " MB"
    } ElseIf ($size -gt 1KB) {
        [math]::Round($size / 1KB, 2).ToString() + " KB"
    } Else {
        $size.ToString() + " B"
    }
}

$folderPath = "C:\MyFolder"
$folderSize = (Get-ChildItem -Path $folderPath -Recurse -File | Measure-Object -Property Length -Sum).Sum
$readableSize = Format-FileSize -size $folderSize
Write-Host "The size of $folderPath is $readableSize"

This script defines a function Format-FileSize that takes a size in bytes and converts it to the largest appropriate unit, then appends the unit abbreviation. It then calculates the folder size and formats it using this function.

Get folder size in GB in PowerShell

To obtain the size of a folder in gigabytes (GB) using PowerShell, you will typically combine the Get-ChildItem cmdlet with Measure-Object. The Get-ChildItem cmdlet retrieves all the items (files and folders) within a specified directory, and Measure-Object will calculate the sum of the sizes of these items. Since the size is calculated in bytes by default, you’ll need to convert it to gigabytes.

Here’s a detailed breakdown of the process:

  1. Retrieve the Items: Use Get-ChildItem to list all the files within the target folder. The -Recurse parameter ensures that the command includes all subfolders and their files.
  2. Measure the Size: Pipe the output of Get-ChildItem to Measure-Object to sum up the Length property of all the files, which represents their size in bytes.
  3. Convert to GB: Since the default output will be in bytes, you need to convert it to gigabytes. There are 1,073,741,824 bytes in a gigabyte (1GB = 2^30 bytes).

Here’s a PowerShell script that encapsulates this process:

# Define the path of the folder you want to measure
$folderPath = "C:\MyFolder"

# Get the size of the folder in bytes
$folderSizeBytes = (Get-ChildItem -Path $folderPath -Recurse -File | Measure-Object -Property Length -Sum).Sum

# Convert the size to gigabytes
$folderSizeGB = [math]::Round($folderSizeBytes / 1GB, 2)

# Output the folder size in GB
Write-Host "The size of the folder is $folderSizeGB GB"

In this script:

  • $folderPath should be replaced with the path to your target folder.
  • The -File parameter is used to ensure that only files are measured, not subdirectories themselves.
  • [math]::Round() is used to round the result to two decimal places for readability.

Remember that this method calculates the size based on the actual data length of the files and doesn’t account for any potential overhead due to file system allocation unit size, which can make the on-disk size slightly larger than the sum of file sizes.

Conclusion

PowerShell provides various methods for determining a folder’s size. In this tutorial, I have explained how to get folder size in GB in PowerShell.

Also, we saw how to display folder sizes in a human-readable format in PowerShell.

You may also like: