Created
January 16, 2025 10:24
-
-
Save Rapidhands/d75c780e141dfb08d2020eb50d5a185a to your computer and use it in GitHub Desktop.
Get-FolderSize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-FolderSize | |
{ | |
<# | |
.SYNOPSIS | |
Calculates the size of a folder in the specified unit. | |
.DESCRIPTION | |
This function calculates the total size of a folder and its contents using the FileSystemObject COM object. | |
It supports various units of measurement (B, KB, MB, GB, TB) and allows customization of decimal places. | |
.PARAMETER Path | |
The path to the folder whose size you want to calculate. Must be a valid folder path. | |
.PARAMETER Unit | |
The unit of measurement for the output size. Valid values are B, KB, MB, GB, and TB. | |
Default value is MB. | |
.PARAMETER Decimals | |
The number of decimal places to round the result to. Must be between 0 and 5. | |
Default value is 2. | |
.OUTPUTS | |
Returns a custom object containing: | |
- Path: The full path of the folder | |
- Size: The calculated size in the specified unit | |
- Unit: The unit of measurement used | |
.EXAMPLE | |
Get-FolderSize -Path "C:\Users" | |
Returns the size of the Users folder in megabytes (default value) with 2 decimal places (default value) | |
.EXAMPLE | |
Get-FolderSize -Path "C:\Users" -Unit GB | |
Returns the size of the Users folder in gigabytes with 2 decimal places (default value) | |
.EXAMPLE | |
"C:\Program Files" | Get-FolderSize -Unit MB -Decimals 0 | |
Returns the size of Program Files folder in megabytes with no decimal places | |
.EXAMPLE | |
Get-Help Get-FolderSize -ShowWindow | |
Full Help about the function in a separate folder | |
.NOTES | |
Author: Olivier .F | |
Version: 1.0 | |
Date : 2023/06/10 | |
Change : v1.0 - Initial version | |
#> | |
[CmdletBinding()] | |
param ( | |
# path of the folder whose size we want to know | |
[Parameter(Mandatory = $true, | |
Position = 0, | |
ValueFromPipeline = $true)] | |
[ValidateScript({ Test-Path $_ -PathType Container })] | |
[string] | |
$Path, | |
# Display unit | |
[Parameter(Position = 1)] | |
[ValidateSet('B', 'KB', 'MB', 'GB', 'TB')] | |
[string] | |
$Unit = 'MB', | |
# Number of decimals to show | |
[Parameter()] | |
[ValidateRange(0, 5)] | |
[int] | |
$Decimals = 2 | |
) | |
begin | |
{ | |
$units = @{ | |
'B' = 1 | |
'KB' = 1KB | |
'MB' = 1MB | |
'GB' = 1GB | |
'TB' = 1TB | |
} | |
} | |
process | |
{ | |
try | |
{ | |
# Using FileSystemObject (COM) is generally faster than Get-ChildItem -Recurse for calculating folder sizes, especially for folders containing many files | |
$folder = Get-Item -Force $Path | |
$fso = New-Object -ComObject Scripting.FileSystemObject | |
$size = $fso.GetFolder($folder.FullName).Size | |
$result = $size / $units[$Unit] | |
$roundedResult = [math]::Round($result, $Decimals) | |
[PSCustomObject]@{ | |
Path = $folder.FullName | |
Size = $roundedResult | |
Unit = $Unit | |
} | |
} | |
catch | |
{ | |
Write-Error "Error calculating size : $_" | |
} | |
finally | |
{ | |
if ($null -ne $fso) | |
{ | |
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($fso) | Out-Null | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using COM objects is incredibly faster on directories with a lot of files.
Not convinced ? Try this function on C:\windows for example.