Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / PowerShell: Get Folder Size on Windows

April 2, 2024 PowerShellWindows 10Windows 11Windows Server 2019

PowerShell: Get Folder Size on Windows

You can use PowerShell to calculate the exact size of a specific folder in Windows (recursively, including all subfolders). This way you can quickly find out the size of the directory on disk without using third-party tools such as TreeSize or WinDirStat. In addition, PowerShell gives you more flexibility to filter or exclude files (by type, size, or date) that you need to consider when calculating folder size.

Use the following PowerShell cmdlets to calculate the size of a folder:

  • Get-ChildItem (gci alias) — gets a list of files (with sizes) in a directory (including nested subfolders). Previously, we showed you how to use the Get-ChildItem cmdlet to find the largest files on the disk.
  • Measure-Object (measure alias) – sums the sizes of all files to get the total directory size.

For example, to find the size of the D:\ISO directory in GB, run:

(Get-ChildItem D:\ISO -force -Recurse -ErrorAction SilentlyContinue| measure Length -sum).sum / 1Gb

Parameters used:

  • -Force – include hidden and system files
  • -Recure – get a list of files in subfolders
  • -ErrorAction SilentlyContinue – ignore files and folders the current user is not allowed to access
  • -measure Length -sum – a sum of all file sizes (the Length property)
  • .sum/ 1Gb – show total size in GB

In this example, the directory size is about 37 GB (this PowerShell command ignores NTFS file system compression if it is enabled).

To round the results to two decimal places, use the command:

"{0:N2} GB" -f ((Get-ChildItem D:\ISO -force -Recurse -ErrorAction SilentlyContinue| measure Length -sum).sum / 1Gb)

PowerShell: get folder size in Gb

PowerShell can find the total size of all files of a particular type in a directory. For example, add the *.iso parameter to find out how much space is taken up by ISO files:

"{0:N2} GB" -f ((Get-ChildItem D:\ISO *.iso -force -Recurse -ErrorAction SilentlyContinue| measure Length -sum).sum / 1Gb)

calculate file size in folder with powershell oneliner

You can use other filters to select files to be included in the directory size calculation. For example, to see the size of files in the directory created in 2024:

(gci -force D:\ISO –Recurse -ErrorAction SilentlyContinue | ? {$_.CreationTime -gt '1/1/24’ -AND $_.CreationTime -lt '12/31/24'}| measure Length -sum).sum / 1Gb

If there are symbolic or hard links in the directory, the above PowerShell cmdlet displays an incorrect folder size. For example, the C:\Windows directory contains many hard links to files in the WinSxS folder (Windows Component Store). Such files may be counted several times. Use the following command to ignore hard links:

"{0:N2} GB" -f ((gci –force C:\Windows –Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LinkType -notmatch "HardLink" }| measure Length -s).sum / 1Gb)

Get the sizes of all top-level subfolders in the destination folder and the number of files in each subfolder (in this example, the PowerShell script displays the size of all user profiles in C:\Users):

$targetfolder='C:\Users'
$dataColl = @()
gci -force $targetfolder -ErrorAction SilentlyContinue | ? { $_ -is [io.directoryinfo] } | % {
    $len = 0
    gci -recurse -force $_.fullname -ErrorAction SilentlyContinue | % { $len += $_.length }
    $filesCount = (gci -recurse -force $_.fullname -File -ErrorAction SilentlyContinue | Measure-Object).Count
  $dataObject = New-Object PSObject -Property @{
        Folder = $_.fullname
        SizeGb = ('{0:N3}' -f ($len / 1Gb)) -as [single]
        filesCount=$filesCount
    }
   $dataColl += $dataObject
   }
$dataColl | Out-GridView -Title "Subfolder sizes and number of files"

get subfolder sizes and file count using PowerShell

% is an alias for the foreach-object loop.

The script will display the Out-GridView graphical table listing the directories, their sizes, and how many files they contain. The folders in the table can be sorted by size or number of files in the Out-GridView form. You can also export the results to a CSV  (| Export-Csv folder_size.csv) or to an Excel file.

These commands work in all versions of PowerShell, including the more recent versions of PowerShell Core 7.x.
20 comments
24
Facebook Twitter Google + Pinterest
previous post
Using Credential Manager on Windows: Ultimate Guide
next post
How to Manually Import (Add) Update into WSUS from Microsoft Update Catalog

Related Reading

How to Cancel Windows Update Pending Restart Loop

May 6, 2025

View Windows Update History with PowerShell (CMD)

April 30, 2025

Change BIOS from Legacy to UEFI without Reinstalling...

April 21, 2025

Remove ‘Your License isn’t Genuine’ Banner in MS...

April 21, 2025

Uninstalling Windows Updates via CMD/PowerShell

April 18, 2025

20 comments

ewolfman July 29, 2019 - 1:24 pm

Great script! Many thanks.
Just adding here: If you want to display the output directly to the screen instead of the grid (for example within a docker container), use:

$dataColl | Write-Output

Reply
Bob March 23, 2020 - 4:16 pm

Thanks for your addition!

Reply
Richard August 6, 2019 - 7:36 pm

Well written article, thanks!

I do have one suggestion, I have to run powershell in a command prompt but can’t run it in a script (running remotely with a tool that doesn’t have access to powershell). The formatting ({0:N2}) and the pipe was not allowing the switches to work. So I had to replace ‘{0:N2}’ with ‘[math]round(…)’. And I had to escape the pipe with a carrot. End result looked like:

[math]round((gci –force c:\Windows –Recurse -ErrorAction SilentlyContinue ^| measure Length -s).sum / 1Gb)

And that got me exactly what I needed. But I wouldn’t have gotten the jump start I needed without this guidance.
Thanks again!

Reply
Luís Palma September 24, 2019 - 11:11 am

Hello,

How can I add a code line for files?

Thank you

Reply
Paresh June 2, 2020 - 3:48 pm

I add below command to get count result as well but it give only one level of sub directory , it doesn’t count sub sub folder file count , can you help with it
Add-Member -InputObject $dataobject -MemberType NoteProperty -name “count” -Value (getchileitem dir $_.FullName -recurse | Measure-Object).Count

Reply
sudharani June 12, 2020 - 4:51 pm

This what I am exactly looking for. One request. How can I export the final result to an .csv.

Reply
Michael Freitas October 6, 2020 - 1:29 pm

I did it manually, selected all the lines, copied and pasted it into excel.
It has already pasted separated by columns.
If you have the script to do this automatically it will be better.

Reply
Nick December 7, 2020 - 4:16 pm

Export-Csv “Path to the CSV file”

Reply
Lukas Novotny September 8, 2020 - 5:46 pm

Last script to show sizes of all subfolders in GUI doesn’t work. Previous script worked, but this just hangs in PS and nothing happens…

Reply
Michael Freitas October 6, 2020 - 1:12 pm

Great Script!!! Thank you!

Reply
Michael Freitas October 6, 2020 - 1:14 pm

I had to manually delete and place double quotes. So it worked!

Reply
Michael Freitas October 6, 2020 - 1:21 pm

Just sorting by size is not working for me. It is classifying as text. It is classifying thus:

97,5
9
80,6
8

But it’s just a detail that doesn’t take away from the script’s merits. Great script!

Reply
Get size of all items in current path – Ivan's Corner January 8, 2021 - 11:00 am

[…] for an easy way to get the size of all user profiles folder through PowerShell and found this woshub and inspired by it ended with the following function which measures the size of everything in […]

Reply
Dattu April 22, 2021 - 8:15 am

Nice code, it workds

Reply
Binjamin Man August 24, 2021 - 12:09 pm

I like the third party tool: Directory Report
It can filter by file types, modification date, size and owner
It can save its output to many file types including directly to MS-Excel

Reply
Krzysztof November 2, 2021 - 9:04 am

Sorting fix:

$targetfolder=’C:\’
$dataColl = @()
gci -force $targetfolder -ErrorAction SilentlyContinue | ? { $_ -is [io.directoryinfo] } | % {
$len = 0
gci -recurse -force $_.fullname -ErrorAction SilentlyContinue | % { $len += $_.length }
$foldername = $_.fullname
$foldersize= [math]::Round(($len / 1Gb),2)
$foldersizeint = [int]$foldersize
$dataObject = New-Object PSObject
Add-Member -inputObject $dataObject -memberType NoteProperty -name “foldername” -value $foldername
Add-Member -inputObject $dataObject -memberType NoteProperty -name “foldersizeGb” -value $foldersize
$dataColl += $dataObject
}
$dataColl | Out-GridView -Title “Size of subdirectories”

Reply
xavi September 26, 2022 - 8:50 am

Unbelievable, a basic daily task needs…. a program? well done MS!! What’s next?
Will you replace the “cls” with a 20-line program to clean the screen?

Reply
Mohsen Gholami December 24, 2022 - 1:49 pm

Actually, cls is an alias to Clear-Host function.
you can find it with this command
Get-Command cls:

CommandType Name Version Source
———– —- ——- ——
Alias cls -> Clear-Host

and get Clear-Host function definition body using this:

Get-Command Clear-Host | select -ExpandProperty Definition

$RawUI = $Host.UI.RawUI
$RawUI.CursorPosition = @{X=0;Y=0}
$RawUI.SetBufferContents(
@{Top = -1; Bottom = -1; Right = -1; Left = -1},
@{Character = ‘ ‘; ForegroundColor = $rawui.ForegroundColor; BackgroundColor = $rawui.BackgroundColor})
# .Link
# https://go.microsoft.com/fwlink/?LinkID=2096480
# .ExternalHelp System.Management.Automation.dll-help.xml

Without its comments, It’s five-line codes.
But I think get folder size is so useful that PowerShell can include a command for it.

Reply
Umair Ahmed March 4, 2023 - 6:27 pm

If you are on a file system that has big cluster size or are trying to find length with byte precision you can’t do that with this script. The cluster size will become your minimum size and all other sizes will be multiples of this.

Unfortunately there is no way in PowerShell to do the same as you do it in explorer. Or at least nobody knows of such.

Reply
Gabriel July 29, 2023 - 11:16 pm

Is it possible to add a member type for file count in each folder? This is perfect and edited a tiny bit to match what we need for planning file server migrations (well at least the planning)

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)
  • How to Download Offline Installer (APPX/MSIX) for Microsoft Store App
  • Configuring Port Forwarding in Windows
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Adding Drivers into VMWare ESXi Installation Image
  • Tracking and Analyzing Remote Desktop Connection Logs in Windows
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top