|
| 1 | +<#PSScriptInfo |
| 2 | +
|
| 3 | +.Version |
| 4 | + 1.0 |
| 5 | +.Guid |
| 6 | + 8b5756da-eb5a-46c7-a388-320817dbd315 |
| 7 | +.Author |
| 8 | + Thomas Malkewitz @tomohulk |
| 9 | +.Tags |
| 10 | + DirectoryInfo, FileInfo, Metadata, Attribute |
| 11 | +.ProjectUri |
| 12 | + https://github.com/dotps1/PSFunctions |
| 13 | +.ReleaseNotes |
| 14 | + Added pipeline support. |
| 15 | + |
| 16 | +#> |
| 17 | + |
| 18 | +<# |
| 19 | +
|
| 20 | +.SYNOPSIS |
| 21 | + Gets the size of a folder. |
| 22 | +.DESCRITPION |
| 23 | + Enumerates all sub folders and file of a directory returning the entire folder structure size. |
| 24 | +.INPUTS |
| 25 | + System.String |
| 26 | +.OUTPUTS |
| 27 | + System.Management.Automation.PSCustomObject |
| 28 | +.PARAMETER Path |
| 29 | + System.String |
| 30 | + The path represtation of the directory to enumerate. |
| 31 | +.PARAMETER Unit |
| 32 | + System.String |
| 33 | + The unit of measurment to return the size value. |
| 34 | +.EXAMPLE |
| 35 | + PS C:\> Get-FolderSize -Path C:\Users\tomohulk\Documents |
| 36 | +
|
| 37 | + Path Size(MB) |
| 38 | + ---- -------- |
| 39 | + C:\Users\tomohulk\Documents 9732.33304 |
| 40 | +.EXAMPLE |
| 41 | + PS C:\> Get-ChildItem -Path C:\Usuers\tomohulk\Documents -Directory | Get-FolderSize -Unit MB |
| 42 | +
|
| 43 | + Path Size(MB) |
| 44 | + ---- -------- |
| 45 | + C:\Users\tomohulk\Documents\Custom Office Templates 0 |
| 46 | + C:\Users\tomohulk\Documents\GitHub 211.29266 |
| 47 | + C:\Users\tomohulk\Documents\GitLab 4.45472 |
| 48 | + C:\Users\tomohulk\Documents\My Received Files 0.24156 |
| 49 | + C:\Users\tomohulk\Documents\WindowsPowerShell 0.07872 |
| 50 | +.LINK |
| 51 | + https://tomohulk.github.io |
| 52 | +
|
| 53 | +#> |
| 54 | + |
| 55 | + |
| 56 | +[CmdletBinding()] |
| 57 | +[OutputType( |
| 58 | + [PSCustomObject] |
| 59 | +)] |
| 60 | + |
| 61 | +param ( |
| 62 | + [Parameter( |
| 63 | + Mandatory = $true, |
| 64 | + ValueFromPipeline = $true, |
| 65 | + ValueFromPipelineByPropertyName = $true |
| 66 | + )] |
| 67 | + [Alias( |
| 68 | + "FullName" |
| 69 | + )] |
| 70 | + [String[]] |
| 71 | + $Path, |
| 72 | + |
| 73 | + [Parameter()] |
| 74 | + [ValidateSet( |
| 75 | + "KB","MB","GB" |
| 76 | + )] |
| 77 | + $Unit = "MB" |
| 78 | +) |
| 79 | + |
| 80 | +process { |
| 81 | + foreach ($pathValue in $Path) { |
| 82 | + if (Test-Path -Path $pathValue) { |
| 83 | + $item = Get-Item -Path ( Resolve-Path -Path $pathValue ) |
| 84 | + if ($item.PSIsContainer) { |
| 85 | + $measure = Get-ChildItem $pathValue -Recurse -Force -ErrorAction SilentlyContinue | |
| 86 | + Measure-Object -Property Length -Sum |
| 87 | + $sum = [Math]::Round( ($measure.Sum / "1$Unit"), 5) |
| 88 | + [PSCustomObject]@{ |
| 89 | + "Path" = $item.FullName |
| 90 | + "Size($Unit)" = $sum |
| 91 | + } |
| 92 | + } else { |
| 93 | + Write-Error -Message "The path '$($item.FullName)' is not a directory." |
| 94 | + continue |
| 95 | + } |
| 96 | + } else { |
| 97 | + Write-Error -Message "Unable to find part of path '$pathValue'." |
| 98 | + continue |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments