Mshah17
Mshah17

Reputation: 103

How to delete all temp files using powershell

Anyone knows how to delete all temp files using powershell.

Get-ChildItem $env:TEMP\TEMP | Remove-Item -confirm:$false -force -Recurse

I tired this code but it couldn't work. Can you suggest me any better way to perform the same.

Upvotes: 10

Views: 39051

Answers (5)

Igor Gregório
Igor Gregório

Reputation: 1

Updated command (+flexive install dirs +concatenation +sequence execution[;]):

$temps = @(“$env:windir\Temp\*”, “$env:windir\Prefetch\*”, “$env:systemdrive\Documents and Settings\*\Local Settings\temp\*”, “$env:systemdrive\Users\*\Appdata\Local\Temp\*”); Remove-Item $temps -force -recurse

Upvotes: 0

TobyU
TobyU

Reputation: 3908

If you don't want to see any errors, you could use the -ErrorAction switch like this:

Remove-Item -Path $env:TEMP\* -Recurse -Force -ErrorAction SilentlyContinue

Upvotes: 13

Kerlc
Kerlc

Reputation: 1

I'm running PS as LOCAdmin and run following command

PS C:\Windows\system32>$tempfolders = @(“C:\Windows\Temp\*”, “C:\Windows\Prefetch\*”, “C:\Documents and Settings\*\Local Settings\temp\*”, “C:\Users\*\Appdata\Local\Temp\*”)
PS C:\Windows\system32>Remove-Item $tempfolders -force -recurse

works for me :)

Upvotes: -2

Rauno Mägi
Rauno Mägi

Reputation: 41

To empty TEMP folder and leave the folder in place, you should use this command:

Remove-Item $env:TEMP\* -Recurse

If you don't want to type so much, you can use also shorter version:

rm $env:TEMP\* -r

Upvotes: 4

Sid
Sid

Reputation: 2676

Just use this:

Remove-Item -Path $env:TEMP -Recurse -Force

Of course you will get access errors if any of the files you are deleting are actually being used by the system.

Upvotes: 1

Related Questions