Adsense

Wednesday, August 29, 2012

PowerShell script to Delete Temporary Internet Files

Wanted to write a quick PowerShell script to delete IIS Temporary Internet Files and thought I would share. If there is a better or less verbose way of writing this, feel free to post a comment. I would love to see other ways of doing it.

$sleepDuration = 5 write-host "Restarting IIS and sleeping $sleep seconds!" Stop-Service W3SVC,WAS -force Start-Sleep -s $sleepDuration write-host "Deleting Temporary Internet Files!" $dirName = "Temporary ASP.NET Files"; $directories = @(gci "c:\windows\Microsoft.Net" -r -force -i $dirName) foreach($x in $directories){ $childDirectories = @(gci $x) foreach($y in $childDirectories){ remove-item "$x\$y" -r -force; } } Start-Service W3SVC,WAS

2 comments:

David Kittell said...

I can't take full credit for it but this script helped me so I thought I'd share it.

https://www.kittell.net/code/powershell-clear-temporary-asp-net-files/

Anonymous said...

You wrote:

Restarting IIS and sleeping $sleep seconds!

and it should be

Restarting IIS and sleeping $sleepDuration seconds!

otherwise your script shows a space where the sleep duration is

--

The original script has a 10-second wait plus you can just use

Remove-ChildItem YourDirectory -Recurse -Force -ErrorAction SilentlyContinue