Created
May 29, 2024 16:14
-
-
Save MattCurryCom/317f41232b83c0f013e2a84b0b35124f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the threshold size in MB | |
$thresholdSizeMB = Read-Host "Enter the minimum file size in MB to list" | |
# Convert the threshold size to bytes | |
$thresholdSizeBytes = $thresholdSizeMB * 1MB | |
# Define the drive to scan | |
$drive = Read-Host "Enter the drive letter to scan (e.g., C:)" | |
# Get a list of all files larger than the threshold size | |
$largeFiles = Get-ChildItem -Path "$drive\" -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $_.Length -gt $thresholdSizeBytes } | |
# Check if any large files are found | |
if ($largeFiles.Count -eq 0) { | |
Write-Host "No files larger than $thresholdSizeMB MB found on drive $drive." | |
} else { | |
# Display the list of large files | |
$largeFiles | Select-Object FullName, @{Name="Size (MB)";Expression={ [math]::round($_.Length / 1MB, 2) }} | Sort-Object @{Expression="Size (MB)";Descending=$true} | Format-Table -AutoSize | |
# Prompt the user to confirm removal of files | |
$confirm = Read-Host "Do you want to delete these files? (y/n)" | |
if ($confirm -eq 'y') { | |
foreach ($file in $largeFiles) { | |
try { | |
Remove-Item -Path $file.FullName -Force | |
Write-Host "Deleted $($file.FullName)" | |
} catch { | |
Write-Host "Failed to delete $($file.FullName): $_" | |
} | |
} | |
} else { | |
Write-Host "File deletion cancelled." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment