PowerShell Genie

Find old temporary files

Script 3 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Get-ChildItem walks the current user's temp directory recursively for files only, with -EA SilentlyContinue swallowing the access denials that a deep walk inevitably hits.

The script

1PS> Get-ChildItem $env:TEMP -File -Recurse -ErrorAction SilentlyContinue | Where-Object LastWriteTime -lt (Get-Date).AddDays(- 30) | Sort-Object Length -Descending | Select-Object FullName,Length,LastWriteTime

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 FullName Length LastWriteTime
    2 -------- ------ -------------
    3 C:\Sample\item.txt 18432 9/10/2026 10:00:00 AM
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    Get-ChildItem $env:TEMP -File -Recurse -ErrorAction SilentlyContinue | Where-Object LastWriteTime -lt (Get-Date).AddDays(- 30) | Sort-Object Length -Descending | Select-Object FullName,Length,LastWriteTime
    1. Stage 1Get-ChildItem-File -Recurse -ErrorAction
    2. Stage 2Where-Object-lt
    3. Stage 3Sort-Object-Descending
    4. Stage 4Select-ObjectPositional values or no named parameters

      How it works

      Get-ChildItem walks the current user's temp directory recursively for files only, with -EA SilentlyContinue swallowing the access denials that a deep walk inevitably hits. Where-Object keeps entries whose LastWriteTime predates a thirty-day cutoff, Sort-Object puts the largest first, and the projection carries the full path with size and age.

      Adapting it

      $env:TEMP resolves to the profile of whoever is running the command. Point it at C:\Windows\Temp instead, since that is usually the directory holding the gigabytes on a server.

      Requirements

      Elevation to read temp directories belonging to other accounts.

      If the result is unexpected

      The command returns almost nothing on a server whose disk is visibly full of temporary data, or it runs for a very long time and throws access errors.

      $env:TEMP expands to the temp directory of whichever account is running the command, which on a server is rarely where the space went. Service accounts and the system profile each have their own. The recursive walk also descends into deep and permission-restricted paths, and the suppressed errors conceal how much it failed to read.

      1. Print $env:TEMP and confirm which profile it resolves to before drawing any conclusion.
      2. Point the scan at C:\Windows\Temp and at the temp directories beneath C:\Users to cover the accounts that matter.
      3. Remove the error suppression temporarily so the access failures become visible.
      4. Add -Depth to bound the recursion when the tree turns out to be unexpectedly deep.
      5. Total the lengths with Measure-Object -Sum to check whether the files found actually account for the missing space.

      Validate the result

      The summed size of the returned files is a meaningful fraction of the space reported as used, and the scan finishes without suppressed access errors.

      Keep the check reliable

      Target temp locations explicitly by path in any cleanup script rather than relying on an environment variable that changes with the calling account.

      Primary documentation

      Get-ChildItem reference

      Continue
      Genie guideFind old temporary files

      Get-ChildItem walks the current user's temp directory recursively for files only, with -EA SilentlyContinue swallowing the access denials that a deep walk inevitably hits.