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.

Check out Convert Bytes to GB 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:
- Retrieve the Items: Use
Get-ChildItemto list all the files within the target folder. The-Recurseparameter ensures that the command includes all subfolders and their files. - Measure the Size: Pipe the output of
Get-ChildItemtoMeasure-Objectto sum up theLengthproperty of all the files, which represents their size in bytes. - 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:
$folderPathshould be replaced with the path to your target folder.- The
-Fileparameter 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.
Get Folder Size and File Count in PowerShell
One of my clients had one requirement to get folder size and file count, which I did using PowerShell.
One of the easiest ways PowerShell provides to find the size of a folder and the number of files it contains is by using the Get-ChildItem cmdlet with Measure-Object. The Get-ChildItem cmdlet retrieves the files and folders in a specified path and Measure-Object calculates the properties of objects, such as the sum of their sizes.
Here is a complete PowerShell script that will give the folder size as well as the files count.
$FolderPath = "C:\MyFolder"
$Files = Get-ChildItem -Path $FolderPath -Recurse -File
$Folders = Get-ChildItem -Path $FolderPath -Recurse -Directory
$TotalSize = ($Files | Measure-Object -Property Length -Sum).Sum
$FileCount = $Files.Count
$FolderCount = $Folders.Count
$TotalSizeInMB = [math]::Round($TotalSize / 1MB, 2)
Write-Host "Total size: $TotalSizeInMB MB"
Write-Host "Number of files: $FileCount"
Write-Host "Number of folders: $FolderCount"After I executed the script using VS code, you can see the output in the screenshot below, it provides the folder size in MB and the files count.

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, I have explained how to get folder size and file count in PowerShell.
Also, we saw how to display folder sizes in a human-readable format in PowerShell.
You may also like:
- PowerShell Foreach File in Folder
- How to Move a File to a Folder in PowerShell?
- How to Copy Files from One Folder to Another in PowerShell?
- How to Check if a Folder is Empty in PowerShell?
- How to Check If a Folder Exists in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.