Post

Detect Large Files on Windows with PowerShell

Detect Large Files on Windows with PowerShell
# Ignore access errors and just list directorys
Get-ChildItem C:\ -Recurse -Force -ErrorAction SilentlyContinue |
ForEach-Object {
if ($_.PSIsContainer) {
$folderSize = (Get-ChildItem $_.FullName -Recurse -Force -ErrorAction SilentlyContinue |
Where-Object { -not $_.PSIsContainer } | # only file
Measure-Object -Property Length -Sum).Sum

[PSCustomObject]@{
Name = $_.Name
DirectoryName = $_.FullName
GB = if ($folderSize) { [Math]::Round($folderSize / 1GB, 2) } else { 0 }
}
} else {
[PSCustomObject]@{
Name = $_.Name
DirectoryName = $_.DirectoryName
GB = [Math]::Round($_.Length / 1GB, 2)
}
}
} |
Where-Object { $_.GB -gt 0 } | # filter 0
Sort-Object -Property GB -Descending | # Sort
Select-Object -First 10 # Select top 10 items

#Basic

# Ignore access errors and just list files
Get-ChildItem C:\ -r -Force -ErrorAction SilentlyContinue |sort -descending -property length | select -first 10 name, DirectoryName, @{Name="GB";Expression={[Math]::round($_.length / 1GB, 2)}}

#Ps Script

# Specified directory:
$targetPath = "C:\"

if (-Not (Test-Path $targetPath)) {
Write-Host "Hedef dizin bulunamadı: $targetPath" -ForegroundColor Red
exit
}
if (-Not (Get-ChildItem -Path $targetPath -File -Recurse -ErrorAction SilentlyContinue)) {
Write-Host "Hedef dizin boş veya erişilemez: $targetPath" -ForegroundColor Yellow
exit
}

# Finding the largest files
$largestFiles = Get-ChildItem -Path $targetPath -Recurse -File -ErrorAction SilentlyContinue |
Sort-Object Length -Descending |
Select-Object @{Name="FullName"; Expression={$_.FullName}},
@{Name="SizeMB"; Expression={[math]::Round($_.Length / 1MB, 2)}} -First 10

# Finding the largest directories
$largestDirectories = Get-ChildItem -Path $targetPath -Recurse -Directory -ErrorAction SilentlyContinue |
ForEach-Object {
$dirSize = (Get-ChildItem -Path $_.FullName -Recurse -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
$_ | Select-Object @{Name="FullName"; Expression={$_.FullName}},
@{Name="SizeMB"; Expression={[math]::Round($dirSize / 1MB, 2)}}
} | Sort-Object SizeMB -Descending -ErrorAction SilentlyContinue |
Select-Object -First 20

# Generate the report
Write-Host "En Büyük 10 Dosya:" -ForegroundColor Cyan
$largestFiles | Format-Table -AutoSize

Write-Host "`nEn Büyük 10 Dizin:" -ForegroundColor Cyan
$largestDirectories | Format-Table -AutoSize

# Saving the report to a file (optional)
$outputFile = "C:\LargeReport_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt"
$reportContent = @"
En Büyük 10 Dosya:
$($largestFiles | Format-Table -AutoSize | Out-String)

En Büyük 10 Dizin:
$($largestDirectories | Format-Table -AutoSize | Out-String)
"@
$reportContent | Set-Content -Path $outputFile

$largestFiles = Get-ChildItem -Path $targetPath -Recurse -File -ErrorAction SilentlyContinue |
Sort-Object Length -Descending |
Select-Object @{Name="DosyaYolu"; Expression={$_.FullName}},
@{Name="Boyut(MB)"; Expression={[math]::Round($_.Length / 1MB, 2)}} -First 10

$largestFiles | Format-Table -AutoSize


Write-Host "`nThe report was saved to file: $outputFile" -ForegroundColor Green
This post is licensed under CC BY 4.0 by the author.