Skip to content

Instantly share code, notes, and snippets.

@Curtis-64
Last active October 10, 2023 00:57
Show Gist options
  • Save Curtis-64/866539d16ec46f0efd63d88eb06aa0c2 to your computer and use it in GitHub Desktop.
Save Curtis-64/866539d16ec46f0efd63d88eb06aa0c2 to your computer and use it in GitHub Desktop.
GetLargestFolders Powershell
//Get Largest Folders by Curtis White + ChatGPT
//PS script copy/paste.
//Not human reviewed but appears to work.
function Get-FolderSize {
param (
[string]$Path
)
Get-ChildItem $Path -Recurse -File -ErrorAction SilentlyContinue |
Measure-Object -Property Length -Sum |
Select-Object @{
Name="Path";
Expression={$Path}
},
@{
Name="SizeMB";
Expression={[math]::Round(($_.Sum / 1MB), 2)}
}
}
function Display-LargestFolders {
param (
[string]$Path,
[int]$Top,
[int]$Level,
[string]$Indent = "",
[double]$MinSizeMB = 2096 # GB
)
$largestFolders = Get-ChildItem $Path -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -ne 'Windows' } | ForEach-Object {
Get-FolderSize -Path $_.FullName
} | Where-Object { $_.SizeMB -gt $MinSizeMB } | Sort-Object @{Expression="SizeMB"; Descending=$true} | Select-Object -First $Top
$largestFolders | ForEach-Object {
Write-Host "$Indent$($_.Path) - $($_.SizeMB) MB"
if ($Level -gt 1) {
Display-LargestFolders -Path $_.Path -Top $Top -Level ($Level - 1) -Indent ("$Indent ") -MinSizeMB $MinSizeMB
}
}
}
function Get-LargestFolders {
param (
[string]$Path = "C:\",
[int]$Top = 20,
[int]$Depth = 5
)
Display-LargestFolders -Path $Path -Top $Top -Level $Depth
}
Get-LargestFolders
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment