Cleaning Week: Find Data Garbage

by May 26, 2016

This week is cleaning week, and we’ll show you tactics how you can potentially retrieve gigabytes of disk space, especially with machines that have been running for a while.

You may know about your personal temp folder ($env:temp), and maybe you even clean it occasionally. Here is a script telling you just how much stuff there is lingering around that hasn’t been touched in more than three months:

# look at temp files older than 3 months
$cutoff = (Get-Date).AddMonths(-3)

$space = Get-ChildItem "$env:temp" -Recurse -Force |
  Where-Object { $_.LastWriteTime -lt $cutoff } |
  Measure-Object -Property Length -Sum |
  Select-Object -ExpandProperty Sum

'Taken space: {0:n1} MB' -f ($space/1MB)

But did you know that Windows has its own temp folder as well? You should clear that one as well once in a while. For that, you do need Administrator privileges. Here is an adaption of the former script, this time checking the Windows temp folder:

#Requires -RunAsAdministrator
# must run with admin privileges!

# look at temp files older than 3 months
$cutoff = (Get-Date).AddMonths(-3)

$space = Get-ChildItem "$env:windir\temp" -Recurse -Force |
  Where-Object { $_.LastWriteTime -lt $cutoff } |
  Measure-Object -Property Length -Sum |
  Select-Object -ExpandProperty Sum

'Taken space: {0:n1} MB' -f ($space/1MB)

Twitter This Tip! ReTweet this Tip!