locked
Deleting Temporary Internet Files RRS feed

  • Question

  • I'm working on a script to delete temporary internet files from all profiles on a given list of servers (namely our terminal servers that keep getting filled up with garbage). I've got it working but I get lots of errors during the deletions because the temp files have file names larger than 260 characters.

    Here's what I have:

    # Declare the servers
    # ===================

    #$tservers = [real server list]



    # @ TESTING BLOCK
    # @
    $tservers = [test machines]
    # @
    # @




    #Display a little info on how many profiles you find
    # ==================================================
    foreach ($server in $tservers) {

        write ''
        write "Server: $Server"
        $profiles = get-childitem "\\$server\C$\Documents and Settings" -force
        write "Found $($profiles.count) profiles on $server."

        # Delete the temporary internet files for each
        # ============================================
        foreach ($userprofile in $profiles) {
        write "Deleting $server\$userprofile temp files..."
        remove-item "$($userprofile.fullname)\local settings\temp\*" -force -recurse
        remove-item "$($userprofile.fullname)\local settings\temporary internet files\content.ie5\*" -force -recurse
        }
    }

    Ideally I'd like to instantiate IE on the remote box and delete the temp files the right way, but I don't know if that's possible in PS. Any better ideas?
    Wednesday, October 28, 2009 2:55 PM

Answers

  • ok, here is another way to avoid this limit - use FileSystemObject COM object:
    $fso = New-Object -ComObject Scripting.FileSystemObject
    $fso.DeleteFolder("path to IE cache folder", $true)

    http://www.sysadmins.lv
    • Marked as answer by Keith Work Thursday, October 29, 2009 2:27 PM
    Wednesday, October 28, 2009 6:20 PM
  • $fso|get-member
    And then you will see a method called deletefile.
    Does it work?

    • Marked as answer by Keith Work Thursday, October 29, 2009 2:28 PM
    Thursday, October 29, 2009 6:05 AM

All replies

  • Well, IE has a COM automation interface:
    PS>$ie=new-object -com internetexplorer.application

    I don't see a way to automate clearing the temp files though from that object though.

    Invoking this remotely could also be an issue, depending on the approach you're considering if you want to try using this COM approach.

    So, in the end, is the only problem the fact that some longer file names are spitting out errors?  That's the only reason you won't try to delete the files directly?
    Wednesday, October 28, 2009 4:02 PM
  • I am deleting the files directly and it is working aside from the long file names, but when I show my manager my spiffy new Powershell script and he runs it, he's going to get a page full of error messages. And all the files will not be deleted which makes me think I'm just delaying the problem until all those overly long file names fill up the disk.

    It's just a can of worms I'd rather not open - I'd like it nice and clean.
    Wednesday, October 28, 2009 5:45 PM
  • ok, here is another way to avoid this limit - use FileSystemObject COM object:
    $fso = New-Object -ComObject Scripting.FileSystemObject
    $fso.DeleteFolder("path to IE cache folder", $true)

    http://www.sysadmins.lv
    • Marked as answer by Keith Work Thursday, October 29, 2009 2:27 PM
    Wednesday, October 28, 2009 6:20 PM
  • Thanks Vadims. Is there a switch or something to delete the contents, not the folder?

    I tried using "delete" with the [path]\* and it kicks back an error: Com object {hex value} doesn't contain a method named 'Delete'.
    Wednesday, October 28, 2009 9:35 PM
  • $fso|get-member
    And then you will see a method called deletefile.
    Does it work?

    • Marked as answer by Keith Work Thursday, October 29, 2009 2:28 PM
    Thursday, October 29, 2009 6:05 AM
  • Ah I didn't think about doing the get-member on the variable. This seems to work (although I still get access denied for the logged on user and the network service account). guess that's the best I can hope for. :)

    Thanks guys!



    # TermScrubber.ps1
    # ===================
    #
    # Connects to each terminal server and cleans off the
    # temporary internet files.
    #
    #


    # Declare the servers
    # ===================

    # $tservers = [real machines]


    # @ TESTING BLOCK
    # @
    $tservers = [test machines]
    # @
    # @


    # Instantiate a file system object
    # (this gets around a problem remove-item has with
    # very long file names like we find in the temp folders)
    # ======================================================
    $fso = New-Object -ComObject Scripting.FileSystemObject


    # Display a little info on how many profiles you find
    # ==================================================
    foreach ($server in $tservers) {

        write ''
        write "Server: $Server"
        $profiles = get-childitem "\\$server\C$\Documents and Settings" -force
        write "Found $($profiles.count) profiles on $server."

        # Delete the temporary internet files for each
        # ============================================
        foreach ($userprofile in $profiles) {

        write "Deleting $server\$userprofile temp files..."
    #    remove-item "$($userprofile.fullname)\local settings\temp\*" -force -recurse
    #    remove-item "$($userprofile.fullname)\local settings\temporary internet files\content.ie5\*" -force -recurse

        $fso.deletefile("$($userprofile.fullname)\local settings\temp\*", $true)
        $fso.deletefile("$($userprofile.fullname)\local settings\temporary internet files\content.ie5\*", $true)
        }
    }

    write ""
    write "Work complete!"
    Thursday, October 29, 2009 2:27 PM
  • Ok I'm getting a strange problem now. I added in a little error checking and now the fso.deletefile stopped working. I see it cycle through every profile but it's not showing an error or actually deleting anything. When I got back to remove-item, it does delete the files (but i get the file length errors again)...

    Gah!



    # TermScrubber.ps1
    # ===================

    <#
    .SYNOPSIS
      Connects to each terminal server and cleans off the
      temporary internet files.
    .DESCRIPTION
      This script connects to a series of servers ($tservers) and locates
      each profile under C:\Documents and Settings. Then deletes the content
      of the following folders from each profile:
     
        [profile]\local settings\temp
        [profile]\local settings\temporary internet files\content.ie5
     
      It does this using a COM object (FileSystemObject) instead of the
      standard "remove-item" commandlet to avoid the path length limitations.
    .NOTES
      File name: TermScrubber.ps1
    .KNOWN ISSUES
      This script will not delete the contents of temp files for profiles
      that are currently in use. Not really an issue but it does result
      in error messages.
    #>


    # Declare the servers
    # ===================

    $tservers = [testbox]


    # Instantiate a file system object
    # (this gets around a problem remove-item has with
    # very long file names like we find in the temp folders)
    # ======================================================
    $fso = New-Object -ComObject Scripting.FileSystemObject


    # Display a little info on how many profiles you find
    # ==================================================
    foreach ($server in $tservers) {

        write ''
        write "Server: $Server"
        $profiles = get-childitem "\\$server\C$\Documents and Settings" -force
        write "Found $($profiles.count) profiles on $server."

        # Delete the temporary internet files for each
        # ============================================
        foreach ($userprofile in $profiles) {

           write "Deleting $server\$userprofile temp files..."
          
           if (test-path "$($userprofile.fullname)\local settings\temp")
                {$fso.deletefile("$($userprofile.fullname)\local settings\temp\*", $true)}
             Else {write "ERROR: $($userprofile.fullname)\local settings\temp does not exist!"  -foregroundcolor red}
            
           if (test-path "$($userprofile.fullname)\local settings\temporary internet files\content.ie5")
                {$fso.deletefile("$($userprofile.fullname)\local settings\temporary internet files\content.ie5\*", $true)}
             Else {write "ERROR: $($userprofile.fullname)\local settings\temporary internet files\content.ie5 does not exist!" -foregroundcolor red}      
          
           #if (test-path "$($userprofile.fullname)\local settings\temp")
           #     {remove-item "$($userprofile.fullname)\local settings\temp\*" -force -recurse}
           #  Else {write "ERROR: $($userprofile.fullname)\local settings\temp does not exist!"}
            
           #if (test-path "$($userprofile.fullname)\local settings\temporary internet files\content.ie5")
           #     {remove-item "$($userprofile.fullname)\local settings\temporary internet files\content.ie5\*" -force -recurse}
           #  Else {write "ERROR: $($userprofile.fullname)\local settings\temporary internet files\content.ie5 does not exist!"}
               
        }
    }

    write ""
    write "Work complete!"
    Thursday, October 29, 2009 8:10 PM
  • this is interesting since i'm a newb to batch files... i'm thinking why not learn powershell right away. i have a winxp/win2k version (win7/vista) that is in bat form. not sure if it wll help set some ideas up that could be converted to ps. the code was influenced from a danish programmer which i modified to cover a much larger amount of files.

    if you have made any update on this please post or share your updated code!

    it also scrubs each user on the machine, which i believe your version does as well correct?


    it looks like -f and -r are converted in your examples above, i'm not sure what these would convert to:

    examples to modify: lokal_sti is a temp variable for local strings, it can be replaced. ~1 is a temp variable for the user that it is currently scanning (it goes through each user).
    basically i redundant the heck out of it:
    1. delete .
    2. delete *.*
    3. delete the directory in some cases and recreate
    then:
    delete temp, temporary internet and java cache (this should be added to your code. alot of dorment viruses can be forced delete to kickstart a machine for a better malwarebytes scan)

    -also tempory internet files comes in several varities at times.
    -could also use win7/vista version for folder path ideas as well. 
    -try with content.ie5 and without!
    -THEN: repeat the code for \temp\ folder prefix examples given below 

    also how are you checking for "verification of deletion"? i relied on ccleaner to do (measure amount of files/size before/after scan of script thens can with ccleaner for differences). i was finding that ccleaner would say it cleaned up several mbs (the same amount my script thought it removed)... only to find that ccleaner was cleaning what was already cleaned, meaning there was nothing to clean but it had a history of it. my script had already cleaned it, and a manual measurement proved this. ccleaner may state it removed 5mb after my script is ran but it removed 0.

    for /d %%j in ("%~1\%Lokal_sti%\Temporary Internet Files\Content.IE5\*") do call :DelTDir "%%j"
    del "%~1\%Lokal_sti%\Temporary Internet Files\Content.IE5\." /f /s /q /a

    for /d %%j in ("%~1\Local Settings\Temporary Internet Files\*") do call :DelTDir "%%j"
    del "%~1\Local Settings\Temporary Internet Files\." /F /S /Q /A
    del "%~1\Local Settings\Temporary Internet Files\*.*" /F /S /Q /A

    temp prefix:
    rem remove all temp files by force
    for /d %%j in ("%~1\%Lokal_sti%\Temp\Temporary Internet Files\*") do call :DelTDir "%%j"
    del "%~1\%Lokal_sti%\Temp\Temporary Internet Files\." /f /s /q
    del "%~1\%Lokal_sti%\Temp\Temporary Internet Files\." /q /a
    
    for /d %%j in ("%~1\%Lokal_sti%\Temp\Temporary Internet Files\Content.IE5\*") do call :DelTDir "%%j"
    del "%~1\%Lokal_sti%\Temp\Temporary Internet Files\Content.IE5\." /F /S /Q /A
    del "%~1\%Lokal_sti%\Temp\Temporary Internet Files\Content.IE5\*.*" /F /S /Q /A

    then folders for a thorough scan:
    echo  *** enter to continue *** CTRL+C to quit
    for /d %%j in ("%~1\Application Data\Sun\Java\Deployment\javaws\cache\*") do call :DelTDir "%%j"
    del "%~1\Application Data\Sun\Java\Deployment\javaws\cache\." /f /s /q /a
    for /d %%j in ("%~1\Application Data\Sun\Java\Deployment\cache\*") do call :DelTDir "%%j"
    del "%~1\Application Data\Sun\Java\Deployment\cache\." /f /s /q /a
    for /d %%j in ("%~1\Application Data\Sun\Java\Deployment\cache\6.0\*") do call :DelTDir "%%j"
    del "%~1\Application Data\Sun\Java\Deployment\cache\6.0\." /f /s /q /a
    
    
    other useful lines that should be replaced with generic variables for various machine types:
    rd c:\windows\TEMP /s /q 
    md C:\windows\TEMP
    rd c:\TEMP /s /q 
    md C:\TEMP

    rd c:\windows\Prefetch /s /q 
    md C:\windows\Prefetch
    
    example with universal variables dose it work in powershell or is there a different method? i'm a newb
    
    ATTRIB %systemdrive%\RECYCLER\* -R -S -H /S /D
    DEL %systemdrive%\RECYCLER\* /F /S /Q 
    RD %systemdrive%\RECYCLER /S /Q 
    Thursday, December 31, 2009 6:27 AM
  • No changes to my script - I'm still using the one above.

    I've used batch files for years but for anyone new to scripting, I'd definitely recommend Powershell. I've just picked it up in the last few months and it's a whole different world from Dos. Especially if you're familiar at all with object-oriented programming.

    Plus with Windows7 and Server 08, Microsoft is strongly pushing Powershell over batch files. You can be ahead of the curve for once. :)
    Thursday, December 31, 2009 3:40 PM
  • yeah would be nice. i thought you were having issues in your last post?

    either way.. if you want a thorough cleanup try my ideas.
    Thursday, December 31, 2009 4:09 PM
  • I realize this is an old thread, but thought I'd post this here anyway.

    A crazy kind of solution we came up with was to use Robocopy's 'PURGE' switch.  We created an 'Empty' folder on the server's data volume then used this command:

    robocopy /PURGE E:\Empty "%LOCALAPPDATA%\Microsoft\Windows\Temporary Internet Files\Content.IE5"

    And it actually works a treat!

    Just hope this helps the next weary traveler.  Cheers!

    -KK

    Friday, July 25, 2014 7:24 PM