Windows OS Hub
  • Windows Server
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Group Policies
  • Windows Clients
    • Windows 10
    • Windows 8
    • Windows 7
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
  • PowerShell
  • Exchange
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Group Policies
  • Windows Clients
    • Windows 10
    • Windows 8
    • Windows 7
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
  • PowerShell
  • Exchange

 Windows OS Hub / PowerShell / PowerShell: Get Folder Sizes on Disk in Windows

March 29, 2019 PowerShell

PowerShell: Get Folder Sizes on Disk in Windows

The most of Windows users get used that the easiest way to check the size of a folder is to open the folder properties in Windows Explorer. More experienced users prefer to use tools like TreeSize or WinDirStat. However, if you want to get a detailed statistics on the size of folders in the specific directory or exclude certain file types, you’d better use the PowerShell scripts. In this article we’ll show you how to quickly get the size of the specific folder on the disk (or all subfolders) using PowerShell.

Tip. To get the size of the specific folder on the disk, you can also use the CLI tool du.exe.

You can use the Get-ChildItem (gci alias) and Measure-Object (measure alias) cmdlets to get the sizes of files and directories in PowerShell.

The first cmdlet allows you to get the list of files in the specified directory by the certain criteria, and the second one performs an arithmetic operation.

Tip. In one of the previous articles we showed how to use Get-ChildItem cmdlet to find the largest files on your computer.

For example, to get the size of c:\ps folder, run the following command:

Get-ChildItem c:\iso | Measure-Object -Property Length -sum

get-childrenitem - powershell - folder size

As you can see, the total size of files in this directory is shown in the Sum field and is about 2.1 GB (the size is given in bytes).

To convert the size into more convenient MB or GB, use this command:

(gci c:\iso | measure Length -s).sum / 1Gb

Or:

(gci c:\iso | measure Length -s).sum / 1Mb

To round the result to two decimals, run the following command:

"{0:N2} GB" -f ((gci c:\iso | measure Length -s).sum / 1Gb)

PowerShell Calculating Folder Sizes - gci and measure sum

To calculate the total size of all files of a certain type in a directory, use this command (for example you want to get the total size of all ISO files in a folder):

(gci c:\iso *.iso | measure Length -s).sum / 1Mb

get folder size in MB using powershell

The commands shown above allow you to get only the total size of files in the specified directory. If there are subfolders in the directory, the size of files in the subfolders won’t be calculated. To get the total size of files in the directory taking subfolders into account, use the –Recurse parameter. Let’s get the total size of files in the C:\Windows directory:

"{0:N2} GB" -f ((gci –force c:\Windows –Recurse -ErrorAction SilentlyContinue| measure Length -s).sum / 1Gb)

To take into account the size of hidden and system files, I have used the –force argument as well.

So the size of C:\Windows on my local drive is about 16 GB.

Tip. In order to prevent directory access errors, use the -ErrorAction SilentlyContinue parameter.

Get-ChildItem - access to the c:\windows\sbs is denied

You can get the size of all first-level subfolders in the specified directory. For example, you want to get the size of all user profiles in the folder C:\Users.

gci -force 'C:\Users'-ErrorAction SilentlyContinue | ? { $_ -is [io.directoryinfo] } | % {
$len = 0
gci -recurse -force $_.fullname -ErrorAction SilentlyContinue | % { $len += $_.length }
$_.fullname, '{0:N2} GB' -f ($len / 1Gb)
}

get user profiles size with powershell

% is an alias for the foreach-object cycle.

Let’s go on. Suppose, your task is to find the size of each directory in the root of the system hard drive and present the information in the convenient table form for analysis and able to be sorted by the folder size. To do it, let’s use the Out-GridView cmdlet.

To get the information about the size of directories on the drive C:\, run the following PowerShell script:

$targetfolder='C:\'
$dataColl = @()
gci -force $targetfolder -ErrorAction SilentlyContinue | ? { $_ -is [io.directoryinfo] } | % {
$len = 0
gci -recurse -force $_.fullname -ErrorAction SilentlyContinue | % { $len += $_.length }
$foldername = $_.fullname
$foldersize= '{0:N2}' -f ($len / 1Gb)
$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”

get folder sizes on drive C: with posh

As you can see, the graphic view of the table should appear where all folders in the root of the system drive C:\ and their size are shown. By clicking the column header, you can sort the folders by size.

14 comments
5
Facebook Twitter Google + Pinterest
previous post
Allow Users to Change Expired Password via Remote Desktop Web Access on Windows Server 2016/2012R2
next post
Converting SCCM WQL Query to SQL

Related Reading

How to Create and Manage Scheduled Tasks with...

April 7, 2021

Updating Windows VM Templates on VMWare with PowerShell

April 5, 2021

Using Native Package Manager (WinGet) on Windows 10

March 24, 2021

How to Cleanup, Truncate or Move Log Files...

March 16, 2021

Using PowerShell Invoke-Command to Run Scripts on Remote...

March 15, 2021

14 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
Even More PowerShell Fun – Zewwy's Info Tech Talks February 13, 2020 - 4:00 am

[…] Getting Folder Sizes […]

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

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange
  • Windows 10
  • Windows 8
  • Windows 7
  • Windows Server 2016
  • Windows Server 2012 R2
  • Windows Server 2008 R2
  • PowerShell
  • VMWare
  • MS Office

Recent Posts

  • How to Disable NetBIOS and LLMNR Protocols in Windows Using GPO?

    April 9, 2021
  • Enable Windows Lock Screen after Inactivity via GPO

    April 8, 2021
  • How to Create and Manage Scheduled Tasks with PowerShell?

    April 7, 2021
  • Updating Windows VM Templates on VMWare with PowerShell

    April 5, 2021
  • Running Multiple IIS Websites on the Same Port or IP Address

    April 1, 2021
  • Can’t Copy and Paste via Remote Desktop (RDP) Clipboard

    March 31, 2021
  • UAC: This App Has Been Blocked for Your Protection on Windows 10

    March 30, 2021
  • How to Unlock a File Locked by Any Process or SYSTEM?

    March 29, 2021
  • Configuring a Domain Password Policy in the Active Directory

    March 26, 2021
  • Using Native Package Manager (WinGet) on Windows 10

    March 24, 2021

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Install RSAT Feature on Demand on Windows 10 1809 and Later
  • Get-ADUser: Getting Active Directory Users Info via PowerShell
  • PSWindowsUpdate: Managing Windows Updates from PowerShell
  • How to Find the Source of Account Lockouts in Active Directory domain?
  • How to Create a UEFI Bootable USB Drive to Install Windows 10 or 7?
  • Managing Printers and Drivers with PowerShell in Windows 10 / Server 2016
  • Get-ADComputer: Find Computer Details in Active Directory with PowerShell
Footer Logo

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


Back To Top