PowerTip: Use PowerShell to Clean Up Temp Files


Summary: Use Windows PowerShell to delete temporary files.

Hey, Scripting Guy! Question How can I use Windows PowerShell to clean up a bunch of files that are stored in my temp directory?

Hey, Scripting Guy! Answer Use the Get-ChildItem cmdlet (dir is an alias), point it to the $env:temp directory, and pipe the results
           to the Remove-Item cmdlet, for example:

dir $env:TEMP -Recurse | Remove-Item -Force -Recurse

Comments (1)

  1. Brian Longworth says:

    Here’s a small modification to remove both the user and system temp files

    Get-ChildItem $env:tmp -Recurse | Remove-Item -Recurse -force -ErrorAction SilentlyContinue
    Get-ChildItem ([environment]::GetEnvironmentVariable("temp","machine")) -Recurse| Remove-Item -Recurse -Force -ErrorAction SilentlyContinue

Skip to main content