Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save techie2000/8ddf0447fdae6c5608dcccedc3d939e5 to your computer and use it in GitHub Desktop.

Select an option

Save techie2000/8ddf0447fdae6c5608dcccedc3d939e5 to your computer and use it in GitHub Desktop.
Reclaim diskspace on Windows
# Check current C: free space so we can measure gains after each step.
Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'" | Select-Object DeviceID,@{Name='FreeGB';Expression={[math]::Round($_.FreeSpace/1GB,2)}},@{Name='UsedGB';Expression={[math]::Round($d.Used/1GB,2)}},@{Name='SizeGB';Expression={[math]::Round($_.Size/1GB,2)}} | Format-Table -AutoSize
# Note, WMI can stall on a busy disk, so a lighter command would be
$d = Get-PSDrive -Name C; [pscustomobject]@{Drive='C:'; FreeGB=[math]::Round($d.Free/1GB,2); UsedGB=[math]::Round($d.Used/1GB,2); TotalGB=[math]::Round(($d.Free+$d.Used)/1GB,2)} | Format-List
# Opening the Storage settings so we can start with the safest built-in cleanup categories.
Start-Process "ms-settings:storage"
# Step 1 (safest, high-yield) — Temporary Files
# This will opened Storage settings for you. In Temporary files, select these first:
# • Windows Update Cleanup
# • Delivery Optimization Files
# • Temporary files
# • DirectX Shader Cache
# • Thumbnails
# • Recycle Bin (only if you don’t need it)
#
# Leave unchecked unless you’re sure:
# • Downloads
# • Previous Windows installation(s)
#
# Then click Remove files.
# run Windows component store cleanup (DISM) to safely reclaim update/component space without touching installer cache files. It can take a while on a busy disk.
# must be run as admin user
DISM /Online /Cleanup-Image /StartComponentCleanup
# clear your user temp files and recycle bin
$temp=$env:TEMP; $before=(Get-ChildItem -LiteralPath $temp -Force -ErrorAction SilentlyContinue | Measure-Object).Count; Get-ChildItem -LiteralPath $temp -Force -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue; $after=(Get-ChildItem -LiteralPath $temp -Force -ErrorAction SilentlyContinue | Measure-Object).Count; Clear-RecycleBin -Force -ErrorAction SilentlyContinue; [pscustomobject]@{TempPath=$temp; ItemsBefore=$before; ItemsAfter=$after; RecycleBinCleared=$true} | Format-List
# Now compare diskspace with earlier value
$d = Get-PSDrive -Name C; [pscustomobject]@{Drive='C:'; FreeGB=[math]::Round($d.Free/1GB,2); UsedGB=[math]::Round($d.Used/1GB,2); TotalGB=[math]::Round(($d.Free+$d.Used)/1GB,2)} | Format-List
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment