Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 654
Mshah17 Avatar asked Jul 26 '18 03:07

Mshah17


People also ask

How do I delete all files in PowerShell?

Delete Files in PowerShell using Remove-Item cmdlet In PowerShell, the Remove-Item cmdlet deletes one or more items from the list. It utilizes the path of a file for the deletion process. Using the “Remove-Item” command, you can delete files, folders, variables, aliases, registry keys, etc.

Can I just delete all temporary files?

Yes, it's safe to delete temporary files from Windows. Most of the time, they'll be deleted automatically — if they're not, you can go in and delete them yourself without any worries.


2 Answers

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
like image 138
TobyU Avatar answered Sep 28 '22 11:09

TobyU


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
like image 41
Rauno Mägi Avatar answered Sep 28 '22 09:09

Rauno Mägi