Research and Development
When disk space gets low, you may want to clean up your temporary folder. This code deletes all files that are older than 30 days to make sure you're not dumping anything that's still needed.
$cutoff = (Get-Date) - (New-TimeSpan -Days 30)
$before = (Get-ChildItem $env:temp | Measure-Object Length -Sum).Sum
Get-ChildItem $env:temp | Where-Object { $_.Length -ne $null } | Where-Object { $_.LastWriteTime -lt $cutoff } | `
Remove-Item -Force -ErrorAction SilentlyContinue -WhatIf # REMOVE -whatif to ENABLE DELETING!
$after = (Get-ChildItem $env:temp | Measure-Object Length -Sum).Sum
'Freed {0:0.00} MB disk space' -f (($before-$after)/1MB)
Since deleting stuff is always risky, I left the -WhatIf
in the code so you can check that you are actually deleting your temp folder and not anything else (due to a typo for example). Once you are comfortable, remove –WhatIf
to invoke the cleanup process. You may be surprised how much garbage can be removed.