DiskCleaner.psm1
|
#Requires -Version 5.1 <# .SYNOPSIS DiskCleaner - A Windows disk cleanup utility module. .DESCRIPTION Provides functions to analyze and clean temporary files, browser caches, and other unnecessary files to free up disk space. #> function Get-TempFileInfo { <# .SYNOPSIS Analyzes temporary files and shows potential space savings. .DESCRIPTION Scans common temporary file locations and reports the total size of files that can be safely removed. .PARAMETER IncludeBrowserCache Include browser cache folders in the analysis. .EXAMPLE Get-TempFileInfo Shows temp file analysis for the current user. .EXAMPLE Get-TempFileInfo -IncludeBrowserCache Includes browser caches in the analysis. #> [CmdletBinding()] param( [switch]$IncludeBrowserCache ) $locations = @( @{ Name = "Windows Temp"; Path = "$env:TEMP" }, @{ Name = "User Temp"; Path = "$env:LOCALAPPDATA\Temp" }, @{ Name = "Windows Prefetch"; Path = "$env:SystemRoot\Prefetch" }, @{ Name = "Thumbnail Cache"; Path = "$env:LOCALAPPDATA\Microsoft\Windows\Explorer" } ) if ($IncludeBrowserCache) { $locations += @( @{ Name = "Chrome Cache"; Path = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache" }, @{ Name = "Edge Cache"; Path = "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache" }, @{ Name = "Firefox Cache"; Path = "$env:LOCALAPPDATA\Mozilla\Firefox\Profiles" } ) } $results = @() $totalSize = 0 foreach ($location in $locations) { if (Test-Path $location.Path) { try { $files = Get-ChildItem -Path $location.Path -Recurse -File -ErrorAction SilentlyContinue $size = ($files | Measure-Object -Property Length -Sum).Sum if ($null -eq $size) { $size = 0 } $results += [PSCustomObject]@{ Location = $location.Name Path = $location.Path FileCount = $files.Count SizeMB = [math]::Round($size / 1MB, 2) } $totalSize += $size } catch { Write-Verbose "Could not access: $($location.Path)" } } } Write-Host "`n=== DiskCleaner Analysis ===" -ForegroundColor Cyan $results | Format-Table -AutoSize Write-Host "Total Potential Savings: " -NoNewline Write-Host "$([math]::Round($totalSize / 1MB, 2)) MB" -ForegroundColor Green Write-Host "($([math]::Round($totalSize / 1GB, 2)) GB)`n" -ForegroundColor Green return $results } function Clear-TempFiles { <# .SYNOPSIS Removes temporary files to free up disk space. .DESCRIPTION Safely removes temporary files from common Windows temp locations. Skips files that are currently in use. .PARAMETER IncludeBrowserCache Also clear browser cache folders. .PARAMETER Force Skip confirmation prompt. .PARAMETER WhatIf Show what would be deleted without actually deleting. .EXAMPLE Clear-TempFiles Cleans temp files with confirmation prompt. .EXAMPLE Clear-TempFiles -Force Cleans temp files without asking for confirmation. .EXAMPLE Clear-TempFiles -WhatIf Shows what would be deleted without deleting anything. #> [CmdletBinding(SupportsShouldProcess)] param( [switch]$IncludeBrowserCache, [switch]$Force ) $locations = @( "$env:TEMP", "$env:LOCALAPPDATA\Temp" ) if ($IncludeBrowserCache) { $locations += @( "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache", "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache" ) } if (-not $Force -and -not $WhatIfPreference) { $confirm = Read-Host "This will delete temporary files. Continue? (Y/N)" if ($confirm -notmatch '^[Yy]') { Write-Host "Operation cancelled." -ForegroundColor Yellow return } } $deletedCount = 0 $deletedSize = 0 $errorCount = 0 foreach ($path in $locations) { if (Test-Path $path) { Write-Host "Cleaning: $path" -ForegroundColor Cyan Get-ChildItem -Path $path -Recurse -File -ErrorAction SilentlyContinue | ForEach-Object { try { $size = $_.Length if ($PSCmdlet.ShouldProcess($_.FullName, "Delete")) { Remove-Item $_.FullName -Force -ErrorAction Stop $deletedCount++ $deletedSize += $size } } catch { $errorCount++ Write-Verbose "Skipped (in use): $($_.Name)" } } } } Write-Host "`n=== Cleanup Complete ===" -ForegroundColor Green Write-Host "Files deleted: $deletedCount" Write-Host "Space freed: $([math]::Round($deletedSize / 1MB, 2)) MB" if ($errorCount -gt 0) { Write-Host "Files skipped (in use): $errorCount" -ForegroundColor Yellow } } function Get-DiskUsage { <# .SYNOPSIS Shows disk usage information for all drives. .DESCRIPTION Displays used and free space for all fixed drives with a visual bar. .PARAMETER Drive Specific drive letter to check (e.g., "C"). .EXAMPLE Get-DiskUsage Shows usage for all drives. .EXAMPLE Get-DiskUsage -Drive C Shows usage for C: drive only. #> [CmdletBinding()] param( [string]$Drive ) $drives = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" if ($Drive) { $drives = $drives | Where-Object { $_.DeviceID -eq "${Drive}:" } } Write-Host "`n=== Disk Usage ===" -ForegroundColor Cyan foreach ($d in $drives) { $usedSpace = $d.Size - $d.FreeSpace $percentUsed = [math]::Round(($usedSpace / $d.Size) * 100, 1) $percentFree = 100 - $percentUsed # Create visual bar $barLength = 30 $usedBars = [math]::Round($barLength * ($percentUsed / 100)) $freeBars = $barLength - $usedBars $color = if ($percentUsed -gt 90) { "Red" } elseif ($percentUsed -gt 70) { "Yellow" } else { "Green" } Write-Host "`nDrive $($d.DeviceID)" -ForegroundColor White Write-Host "[" -NoNewline Write-Host ("=" * $usedBars) -NoNewline -ForegroundColor $color Write-Host ("." * $freeBars) -NoNewline -ForegroundColor DarkGray Write-Host "] $percentUsed% used" Write-Host " Total: $([math]::Round($d.Size / 1GB, 2)) GB" Write-Host " Used: $([math]::Round($usedSpace / 1GB, 2)) GB" -ForegroundColor $color Write-Host " Free: $([math]::Round($d.FreeSpace / 1GB, 2)) GB" -ForegroundColor Green } Write-Host "" } # Export functions Export-ModuleMember -Function Get-TempFileInfo, Clear-TempFiles, Get-DiskUsage |