How to Get Folder Size in PowerShell Including Subfolders

A file server’s free space can disappear faster than expected. I have seen a small company’s D:\CompanyData drive fill up because old application logs, user exports, and archived project files kept growing unnoticed.

The Windows folder Properties dialog works for one quick check, but it does not scale when you need to inspect multiple folders or create a repeatable report. PowerShell gives you a faster way to calculate folder size, include subfolders, and identify where storage goes.

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.

In this guide, I will show you how to get folder size in PowerShell, build a reusable script, and export a folder-size report for a file server.

PowerShell Get Folder Size Basics

A folder does not store a single “size” value you can directly read. Its total size comes from the files inside it. To calculate the size, PowerShell must find files and add their byte lengths together.

The key cmdlet is Get-ChildItem. A cmdlet is a built-in PowerShell command that follows a verb-noun naming pattern. Get-ChildItem retrieves files and folders from a path.

For our example, assume the file server stores department data here:

D:\CompanyData

Start by checking the size of files directly inside the folder:

Get-ChildItem -Path "D:\CompanyData" -File |
Measure-Object -Property Length -Sum

This command returns the combined file size in bytes.

Here is what each line does:

  • Get-ChildItem reads the items in D:\CompanyData.
  • -Path tells the cmdlet which folder to inspect.
  • -File limits results to files and excludes subfolders.
  • The pipeline character (|) sends the file objects to the next command.
  • Measure-Object adds the Length property, which stores each file’s size in bytes.
  • -Sum returns the total.

This command does not include nested folders. That detail matters because file servers usually store data several levels deep.

For a broader introduction to the cmdlet, see how to use Get-ChildItem in PowerShell.

Get Folder Size in PowerShell Including Subfolders

To calculate a folder’s complete size, add the -Recurse parameter. A parameter changes how a cmdlet works. In this case, -Recurse tells Get-ChildItem to walk through every subfolder.

Get-ChildItem -Path "D:\CompanyData" -File -Recurse |
Measure-Object -Property Length -Sum

The output includes a Sum property, but the number appears in bytes. That format is accurate but not practical for daily administration.

Use this version to display the result in gigabytes:

$FolderPath = "D:\CompanyData"

$FolderSize = Get-ChildItem -Path $FolderPath -File -Recurse |
Measure-Object -Property Length -Sum

"{0:N2} GB" -f ($FolderSize.Sum / 1GB)

This script produces output similar to:

82.47 GB

The first line creates the $FolderPath variable. A variable stores a value so you can change the folder path once instead of editing several commands.

The second block gets every file under that path and calculates the total bytes. The final line divides that total by 1GB, then formats the number to two decimal places with "{0:N2} GB" -f.

PowerShell recognizes byte-size constants such as 1KB, 1MB, 1GB, and 1TB. You can also use the PowerShell convert bytes to GB guide when you need more formatting options.

Pro Tip: In my experience, a recursive folder scan can take time on a large file server, especially across a network share. Test the command on one department folder first, then run the full scan during a quieter period.

the folder in PowerShell. Let us discuss each and every method with examples.

Get Folder Size in PowerShell 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

Display 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.

Get Folder Size and File Count in PowerShell

One of my clients had a 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 file count.

Get Folder Size and File Count in PowerShell

Create a Reusable Folder Size Script

A one-line command helps with quick checks. A script file helps when you need a consistent process that other administrators can run safely.

Create a file named Get-FolderSize.ps1 and add this script:

param(
[Parameter(Mandatory)]
[string]$FolderPath
)

if (-not (Test-Path -Path $FolderPath -PathType Container)) {
Write-Error "The folder path does not exist: $FolderPath"
exit 1
}

$Files = Get-ChildItem -Path $FolderPath -File -Recurse -ErrorAction SilentlyContinue

$SizeInBytes = ($Files | Measure-Object -Property Length -Sum).Sum

[PSCustomObject]@{
FolderPath = $FolderPath
FileCount = $Files.Count
SizeInMB = [math]::Round($SizeInBytes / 1MB, 2)
SizeInGB = [math]::Round($SizeInBytes / 1GB, 2)
}

Run the script from the folder where you saved it:

.\Get-FolderSize.ps1 -FolderPath "D:\CompanyData"

The script returns an object with the folder path, file count, megabytes, and gigabytes. An object is structured data that PowerShell can display, sort, filter, or export.

Here is why each part matters:

  • param() creates a required FolderPath input instead of hard-coding a path.
  • Test-Path confirms that the supplied path exists and is a folder.
  • -PathType Container specifically checks for a directory, not a file.
  • -ErrorAction SilentlyContinue skips files that PowerShell cannot read, such as locked files or restricted folders.
  • $Files.Count returns the number of files that PowerShell scanned.
  • PSCustomObject creates clean, named output properties.
  • [math]::Round() limits the displayed size to two decimal places.

If a protected folder causes an access error, review how to fix “access to the path is denied” in PowerShell.

This script works in Windows PowerShell 5.1 and PowerShell 7. For file server administration, Windows PowerShell 5.1 remains common. PowerShell 7 works well too, but test scripts against your server environment before standardizing them.

Check Each Top-Level Folder

Knowing that D:\CompanyData uses 82 GB is useful. Knowing that the Finance, Projects, or Logs folder consumes most of it is more useful.

Use the following script to calculate the size of every top-level folder:

$RootPath = "D:\CompanyData"

Get-ChildItem -Path $RootPath -Directory | ForEach-Object {
$Folder = $_

$SizeInBytes = (
Get-ChildItem -Path $Folder.FullName -File -Recurse -ErrorAction SilentlyContinue |
Measure-Object -Property Length -Sum
).Sum

[PSCustomObject]@{
FolderName = $Folder.Name
FolderPath = $Folder.FullName
SizeInGB = [math]::Round($SizeInBytes / 1GB, 2)
}
} | Sort-Object -Property SizeInGB -Descending

This produces a list with the largest folders first.

The script begins with Get-ChildItem -Directory, which returns only the first-level folders under D:\CompanyData. ForEach-Object then runs the size calculation for each folder.

$Folder.FullName passes the full path into the recursive scan. Finally, Sort-Object with -Descending puts the largest folder at the top.

This approach works well for department shares, backup locations, application data, and log roots. If you need to locate data by filename before checking its size, use PowerShell file name wildcard examples.

Export Folder Sizes to CSV

A CSV report creates a useful storage baseline. You can save the report monthly, compare growth, and share the data with a manager without giving them server access.

Add Export-Csv to the previous script:

$RootPath = "D:\CompanyData"
$ReportPath = "C:\Reports\CompanyData-FolderSizes.csv"

$Report = Get-ChildItem -Path $RootPath -Directory | ForEach-Object {
$Folder = $_

$SizeInBytes = (
Get-ChildItem -Path $Folder.FullName -File -Recurse -ErrorAction SilentlyContinue |
Measure-Object -Property Length -Sum
).Sum

[PSCustomObject]@{
FolderName = $Folder.Name
FolderPath = $Folder.FullName
FileCount = (Get-ChildItem -Path $Folder.FullName -File -Recurse -ErrorAction SilentlyContinue).Count
SizeInGB = [math]::Round($SizeInBytes / 1GB, 2)
ScanDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
}

$Report |
Sort-Object -Property SizeInGB -Descending |
Export-Csv -Path $ReportPath -NoTypeInformation

Write-Host "Report saved to $ReportPath"

Export-Csv writes PowerShell objects into a spreadsheet-friendly CSV file. -NoTypeInformation removes an unnecessary type-information line from the top of the file.

I include ScanDate because a size report without a date becomes difficult to compare later. I also use a separate $Report variable so I can review the data onscreen before exporting it.

For more reporting patterns, read how to export data with PowerShell Export-Csv.

Things to Keep in Mind

  • Use -File when measuring size: Folder objects do not have useful file-length values, so include only files in the calculation.
  • Expect long scan times: -Recurse must inspect every reachable file, which can take several minutes on large or remote shares.
  • Handle denied paths: Use -ErrorAction SilentlyContinue for reporting, but review skipped paths if accuracy matters.
  • Check the path first: Use Test-Path before a scan so a typo does not produce a misleading empty result.
  • Avoid duplicate scans: Store file results in a variable when you need both file count and total size.
  • Run with suitable permissions: Use an account that can read every folder you want to include in the report.

Frequently Asked Questions

How do I get a folder size in MB with PowerShell?

Divide the total byte count by 1MB instead of 1GB. For example, use [math]::Round($FolderSize.Sum / 1MB, 2). This returns a more useful value for smaller folders.

Does Get-ChildItem include subfolders by default?

No. Get-ChildItem reads only the selected folder by default. Add the -Recurse parameter to include files inside all child folders.

Why does my folder size show as zero?

The path may contain only subfolders, while your command lacks -Recurse. You may also lack permission to read the files. Confirm the path with Test-Path and run the command with an account that has read access.

Can I get folder sizes from a network share?

Yes. Replace the local path with a UNC path, such as \\FileServer01\Departments. The account running PowerShell needs permission to read the share and its subfolders.

How do I find the largest folders in PowerShell?

Calculate each top-level folder’s size as a custom object, then pipe the results to Sort-Object -Property SizeInGB -Descending. The largest folder appears first.

You now know how to get folder size in PowerShell, include subfolders, identify large department folders, and export the results to CSV. Start with one folder, test carefully, and then automate more once you are confident. I hope you found this article helpful.

You May Also Like

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.