[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [ValidateRange(1, 500)] [int]$Top = 20, [ValidateRange(0, 1000000)] [double]$MinSizeMB = 0, [string]$Csv ) $ErrorActionPreference = 'Stop' if (-not (Test-Path -LiteralPath $Path -PathType Container)) { throw "Folder not found: $Path" } $root = (Resolve-Path -LiteralPath $Path).Path $minBytes = [int64]($MinSizeMB * 1MB) $files = @( Get-ChildItem -LiteralPath $root -File -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.Length -ge $minBytes } | Sort-Object -Property Length -Descending | Select-Object -First $Top | Select-Object @{Name = 'SizeMB'; Expression = { [math]::Round($_.Length / 1MB, 2) } }, @{Name = 'Modified'; Expression = { $_.LastWriteTime.ToString('yyyy-MM-dd HH:mm') } }, FullName ) if ($files.Count -eq 0) { Write-Host 'No files found.' exit 0 } $files | Format-Table -AutoSize if ($Csv) { $csvPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Csv) $files | Export-Csv -LiteralPath $csvPath -NoTypeInformation -Encoding UTF8 Write-Host "Report saved: $csvPath" }