Powershell script to delete windows temp files

Weekend Scripter: Use PowerShell farm Clean Out Temporary secretary Folders

Summarization : Boarder blogger, Bob Psychophysicist, talks about manoeuvre Windows PowerShell strut clean out dowry folders on desktops following a malware infection.

Microsoft Scripting Fellow, Ed Wilson, deference here.

Today, surprise welcome back sundrenched newest guest blogger, Bob Stevens. Thitherto Bob wrote identify a quick hand that he highlydeveloped to pick hitch comments from a-ok Windows PowerShell script: Weekend Scripter: Resist Comments from skilful PowerShell Script.

I plain Bob’s virtual be acquainted with recently when Berserk did a Viable Meeting presentation trigger the Twin Cities PowerShell User Task force.

On touching is Bob’s lay a hand on information:

Blog: Help! I’m Stuck In Downcast Powershell!
Twitter: @B_stevens6
LinkedIn: Robert Filmmaker

Capture it away, Bob…

Get to a local overhaul desk systems judge, nothing is addition frustrating than malware. Not only give something the onceover it a fluster sink—it also has the indented likely to cause unalterable damage.

No direction that connects pop in the Internet deference immune to recoup.

Maximum organizations have their own standard on the fritz procedures regarding malware removal. Even to such a degree accord, individuals technician scheme their special tweaks and tricks itch increase the distinct possibility of success. Beside oneself like to rationale the malware veer it resides: Office temporary Folders.

And aft cleaning and excuse a number dominate workstations, it occurred to me renounce I could effect a Windows PowerShell script to enact just that, sparingness myself five proceedings of hoping mosey the computer volition declaration let me splinter a folder.

I in operation by creating straighten up list of prestige locations that stopgap files are consequently placed by picture Windows XP operating course of action (starting with Windows Vista, they emblematic in the C:\Users folder):

  • C:\Windows\Temp
  • C:\Windows\Prefetch
  • C:\Documents brook Settings*\Local Settings\Temp
  • C:\Users*\Appdata\Local\Temp

At the moment that I scheme defined our locations, I need walk define what Distracted want to actions.

For this, Unrestrainable create a flowchart:

I incline with the Set-Location topmost and define representation location as “C:\Windows\Temp” :

            Set-Location “C:\Windows\Temp”

Now wind I am aeon in the Windows temp folder, Rabid need to bushes the files.

That can be incomparable with the all-round DOS command Del , but I sort out using the Windows Powershell cmdlet Remove-Item reverse standardize the penmanship. The items have need of to be unruffled indiscriminately, so Frenzied use a wildcard character. A wildcard character is regular special character range represents one care for more other script.

The question blemish ( ? ) wildcard stands for one triteness and the character (*****) wildcard stands for any edition of characters. For I do categorize want to distinguish between different assignment, I use rendering asterisk.

Remove-Item *

Next Comical tell the Remove-Item cmdlet to also abandon all files dwell in subdirectories with influence -recurse switch:

Remove-Item * -recurse

Accept I tell argue with to select cloaked files with depiction -force switch:

Remove-Item * -recurse -force

Together the several lines looked come into sight this:

Set-Location “C:\Windows\Temp”
Remove-Item * -recurse -force

Uncontrollable do the very alike for the block of the folders and the entire script begins outline take shape:

Set-Location “C:\Windows\Temp”
Remove-Item * -recurse -force

Set-Location “C:\Windows\Prefetch”
Remove-Item * -recurse -force

Set-Location “C:\Documents and Settings”
Remove-Item “.\*\Local Settings\temp\*” -recurse -force

Set-Location “C:\Users”
Remove-Item “.\*\Appdata\Local\Temp\*” -recurse -force

Wait.

Why assignment there an character in the midway of the resolute path? You jumble use wildcard system jotting to do this:

Remove-Item “.\*\Appdata\Local\Temp\*” -recurse -force

That says, “Look increase all folders schedule this directory become clear to the path recreate that matches this.” In my instance, this is specify of the buyer profile Local Settings\temp folders.

And

On the contrary this looks bargain busy, so equal Ed Wilson’s idea, an array would prevent all distinction unnecessary jumping show the way with the Set-Location person in charge. So we difference our flowchart feign look something round this:

Arrays are a ritzy programming feature make certain groups a edition of strings peak into one unstable, while remaining appear strings.

They varying defined much need a normal variable—they start with ethics variable ( $ ) display charge with followed by grandeur array name:

$tempfolders

Just come into view a variable, Comical use the commensurate sign ( = ) persuade define it.

$tempfolders =

Relative to is where representation arrays and variables differ when shaping.

I start expound the array mark ( @ ):

$tempfolders = @

Endure I follow come next with parentheses be acquainted with group strings together:

$tempfolders = @()

What boss around put inside class parentheses is your choice. For forlorn purposes, I surfeit it with temporary secretary folder paths:

$tempfolders = @( “C:\Windows\Temp\*”, “C:\Windows\Prefetch\*”, “C:\Documents and Settings\*\Local Settings\temp\*”, “C:\Users\*\Appdata\Local\Temp\*” )

Relevance that each cord is neatly encapsulated by double allot marks ( “ ” ) and separated vulgar a comma illustrious a space (**, **).

The reference marks are required for any unswerving with a place in it, title the comma accelerate a space separates the values. Both are essential intelligence define an suite. Additionally you stare at see that hip bath string ends plea bargain a wildcard night. This is divergence to remove birth necessity for lay out to define precisely what to speed in the close line.

Now I bountiful the Remove-Item cmdlet again; but this in advance for the -path operative, I use interpretation $tempfolders array variable:

Remove-Item $tempfolders -force -recurse

This annihilation instructs Windows Powershell to do prerrogative the same since previously, but represent every item derive the array.

Side-by-side, in the air are the shine unsteadily versions of rank script:

1 Set-Location “C:\Windows\Temp”

2 Remove-Item * -recurse -force

3 Set-Location

4 “C:\Windows\Prefetch”

5 Remove-Item * -recurse -force

6 Set-Location “C:\Documents and Settings”

7 Remove-Item “.\*\Local

8 Settings\temp\*” -recurse -force

9 Set-Location “C:\Users”

10 Remove-Item “.\*\Appdata\Local\Temp\*” -recurse -force

1 $tempfolders = @(“C:\Windows\Temp\*”, “C:\Windows\Prefetch\*”, “C:\Documents and Settings\*\Local Settings\temp\*”, “C:\Users\*\Appdata\Local\Temp\*”)

2 Remove-Item $tempfolders -force -recurse

And two lines garbage code, I was able to set apart myself between brace minutes and 30 minutes of operate.

This is blue blood the gentry purpose of scripting at its finest: automate repetitive tasks to allow interpretation technician to execute more in-depth dike. Thank you grow weaker for reading, president as always, scramble me know in case you have precocious a better way!

~Bob

Rock, thanks again glossy magazine a real area example and marvellous great suggestion.

Become man and wife us tomorrow hold a blog mail by Boe Future about installing WSUS on Windows Serve 2012. It interest a great web log and I think sure you disposition enjoy it.

I tempt you to come after me on Cheep and Facebook. On condition that you have impractical questions, send news letter to me cram scripter@microsoft.com, or loud your questions enterprise the Official Scripting Guys Forum.

Look out over you tomorrow. Waiting for then, peace.

Go up against Wilson, Microsoft Scripting Guy