Most Windows users know that the easiest way to check the size of a folder is to open the folder properties in File Explorer. More experienced users prefer to use third-party tools like TreeSize or WinDirStat. However, if you want to get more detailed statistics on the size of folders in the specific directory or exclude certain file types, you’d better use the PowerShell features. In this article, we’ll show you how to quickly get the size of the specific folder on the disk (and all subfolders) using PowerShell.
You can use the Get-ChildItem (gci
alias) and Measure-Object (measure
alias) cmdlets to get the sizes of files and folders (including subfolders) in PowerShell. The first cmdlet allows you to get the list of files (with sizes) in the specified directory according to the specified criteria, and the second one performs arithmetic operations.
For example, to get the size of the C:\ISO folder, run the following command:
Get-ChildItem C:\ISO | Measure-Object -Property Length -sum
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 a 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)
You can use PowerShell to calculate the total size of all files of a certain type in a directory. 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
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 including subdirectories, use the –Recurse
parameter. Let’s get the total size of files in the C:\Windows folder :
"{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 40 GB (script ignores NTFS compression).
-ErrorAction SilentlyContinue
parameter.This script incorrectly calculates the size of a directory if it contains symbolic or hard links. For example, the C:\Windows
folder contains many hard links to files in the WinSxS folder (Windows Component Store). As a result, such files can be counted several times. To ignore hard links in the results, use the following command (takes a long time to complete):
"{0:N2} GB" -f ((gci –force C:\windows –Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LinkType -notmatch "HardLink" }| measure Length -s).sum / 1Gb)
As you can see, the actual size of the Windows folder is slightly smaller.
You can use filters to select the files to consider when calculating the final size. For example, you can get the size of files created in 2020:
(gci -force c:\ps –Recurse -ErrorAction SilentlyContinue | ? {$_.CreationTime -gt ‘1/1/20’ -AND $_.CreationTime -lt ‘12/31/20’}| measure Length -s).sum / 1Gb
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)
}
%
is an alias for the foreach-object
loop.
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 get the information about the size of directories on the system C:\ drive, 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”
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 (the table is generated by the Out-GridView
cmdlet). By clicking the column header, you can sort the folders by size. You can also export the results to CSV (| Export-Csv folder_size.csv
) or to an Excel file.
If you are using directory size checking in your PowerShell scripts, you can create a separate function:
function Get-FolderSize {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
$Path
)
if ( (Test-Path $Path) -and (Get-Item $Path).PSIsContainer ) {
$Measure = Get-ChildItem $Path -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum
$Sum = '{0:N2}' -f ($Measure.Sum / 1Gb)
[PSCustomObject]@{
"Path" = $Path
"Size($Gb)" = $Sum
}
}
}
To use the function, simply run the command with the folder path as an argument:
Get-FolderSize ('C:\PS')
You can use your local PowerShell function to check the folder size on remote computers via the Invoke-Command
(PowerShell Remoting) cmdlet.
Invoke-Command -ComputerName hq-srv01 -ScriptBlock ${Function:Get-FolderSize} –ArgumentList 'C:\PS'