Skip to content

Instantly share code, notes, and snippets.

@Sverigevader
Created November 10, 2024 11:59
Show Gist options
  • Save Sverigevader/0ad1fb8fde9c2d448725e71df5b5d5df to your computer and use it in GitHub Desktop.
Save Sverigevader/0ad1fb8fde9c2d448725e71df5b5d5df to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
I wrote this to replace my reliance on WinDirStat. I use it to free up space on my computer when needed.
.DESCRIPTION
The script will default to C:\ unless specified. It will ignore system folders by default
Currently they are:
- "C:\Windows",
- "C:\`$Recycle.Bin",
- "C:\System Volume Information"
.PARAMETER Path
The path to scan for files in
.PARAMETER CsvPath
Output path for the resultset
.EXAMPLE
.\Find-LargeFiles.ps1 -Path "C:\" -CsvPath "C:\temp\out.csv" -ConsoleOutput
.EXAMPLE
.\Find-LargeFiles.ps1 -Path "C:\" -CsvPath "C:\temp\out.csv"
.EXAMPLE
.\Find-LargeFiles.ps1 -Path
.NOTES
Author: Sverigecader
Website: http://juliusrobert.site
Twitter: @sverigevader
#>
[CmdletBinding()]
param(
[Parameter(Position=0)]
[string]$Path = "C:\",
[Parameter()]
[string]$CsvPath,
[Parameter()]
[switch]$ConsoleOutput
)
$excludeFullPaths = @(
"C:\Windows",
"C:\`$Recycle.Bin",
"C:\System Volume Information"
)
$minimumSizeMB = 1000
$sizeThreshold = $minimumSizeMB * 1MB
$sizeDescription = "$minimumSizeMB MB"
Write-Host "Scanning $Path for files larger than $sizeDescription (excluding system directories)..." -ForegroundColor Yellow
try {
$files = Get-ChildItem -Path $Path -File -Recurse -ErrorAction SilentlyContinue
| Where-Object {
$file = $_
($_.Length -ge $sizeThreshold) `
-and ($excludeFullPaths | ForEach-Object { -not $file.FullName.StartsWith($_) })
}
| Select-Object @{
Name='Size (MB)'
Expression={[math]::Round($_.Length / 1MB, 2)}
},
FullName,
LastWriteTime
| Sort-Object 'Size (MB)' -Descending
if ($files.Count -eq 0) {
Write-Host "No files found larger than $sizeDescription." -ForegroundColor Yellow
} else {
if ($ConsoleOutput -eq $true) {
$files | Format-Table -AutoSize
Write-Host "Found $($files.Count) files." -ForegroundColor Green
}
if ($CsvPath) {
$files | Export-Csv -Path $CsvPath -NoTypeInformation -Force
Write-Host "Results exported to: $CsvPath" -ForegroundColor Green
}
}
}
catch {
Write-Host "An error occurred: $_" -ForegroundColor Red
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment