[Updated] Powershell script to clear browser caches for every user on a terminal server (Google Chrome, Mozilla, IE)

Hi All,

I thought I’d share this little gem with you.  It’s something I’ve written to clear the browser caches for all users (yes!! all users, simultaneously) on a terminal server [Link Below] – works on desktops as well (or any other directories, just modify the script appropriately).  It takes around 30 seconds to 5 mins to complete (user # dependant).  It will save you disk space and precious time.

This is great for squeezing the last ‘bits’ of space from your terminal server. In my case, we have a 10% threshold for space remaining and if I’m getting towards 9% or 8% left with no real space hogs, this script can claw me back above 10%, sometimes regaining 2GB or 3GB  of space, but I have cleared upto 30GB!!!!

This saves you hours of time trawling user data for tit-bits of space.  Place in a scheduled task for automation.

Before you run this, please read the script and gain an understanding of it’s purpose. I cannot be held responsible for your decision to run this.  This has been tried and tested but you must understand that your environment will be unique.

(It may need a little tweaking for your specific environment. The script clears Google, Mozilla and IE caches as well as temporary Windows directories.  Ensure it is OK to delete these before running the script.  The recycle bin is also emptied. Remove lines from the script if you do not wish for certain directories to be cleaned.)

I have moved this into my Github.  You can find the script here:

https://github.com/lemtek/Powershell/blob/master/Clear_Browser_Caches

All I ask is that you don’t republish this – Please link people back to this page.  Feel free to send me a small donation if this has helped you. It will enable me to continue development of this and other tools. Thank you.

Dontate Here

 

 

Advertisements

39 thoughts on “[Updated] Powershell script to clear browser caches for every user on a terminal server (Google Chrome, Mozilla, IE)”

  1. This looks like a very useful script, but I have not been able to verify that it has worked. I have exported the csv of users and configured the script to work on one of my terminal servers. It does run, and completes, but does not seem to do anything. Is there any way this can be modified to output results to a file, or could it simply tell me whether it was successful or not? I’m not a powershell guy by trade so I’m not sure how to troubleshoot this. Thanks!

    1. Hi Ryan, thanks for getting in touch. I do have an updated version of this script I’ll post up tomorrow if I remember. It simply adds a ‘-verbose’ flag onto each line of code that writes the output to the powershell console. This will show you in real time what’s happening. It is a little quick to read so you could add ‘pause’ to the end of the script to enable you to scroll through the output. It is also possible to write the output to a file but I find that method works for me.

      You also need to be clear on the function of this script, it clears Google Chrome, Firefox, IE and Windows caches and temp files. If these directories are not considerably full, or your TS does not house many users, you may find this doesn’t actually do much.

  2. Spotted another issue – it wasn’t clearing the Mozilla Cache2 folder until I amended this line …. to use * instead of *.*
    Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache2\entries\* -Recurse -Force -EA SilentlyContinue -Verbose

    1. Thanks for the notice Stephen 🙂 I have confirmed this and updated the script. You probably don’t need the reason, but for those that are curious, the cache2 folder only contains files without an extension. You may need to make this adjustment elsewhere that this occurs as there were no errors reported using the *.* wildcard. Just something to be aware of.

  3. Thanks for the script. I ended up modifying it a little to avoid making a CSV and to give a little more granular progress without the crazy output.
    Oh you also have: Write-Host -ForegroundColor yellow “Clearing Google caches”
    under the IE Section 😛

    get-childitem c:\users\ | foreach {
    Write-Host -ForegroundColor yellow “Clearing IE Cache for” $_
    Remove-Item -path “C:\Users\$_\AppData\Local\Microsoft\Windows\Temporary Internet Files\*” -Recurse -Force -EA SilentlyContinue
    Remove-Item -path “C:\Users\$_\AppData\Local\Microsoft\Windows\WER\*” -Recurse -Force -EA SilentlyContinue
    Remove-Item -path “C:\Users\$_\AppData\Local\Temp\*” -Recurse -Force -EA SilentlyContinue
    Write-Host -ForegroundColor yellow “Clearing GoToMeeting for” $_
    Remove-Item -path “C:\Users\$_\AppData\Local\Citrix\GoToMeeting\*” -Recurse -Force -EA SilentlyContinue -Verbose
    }

  4. I changed “Exporting the list of users to c:\users\%username%\users.csv” to “Exporting the list of users to c:\users\$env:USERNAME\users.csv”

    Now it shows the correct path of the stored file.

  5. I used ICSweep but it only clears IE cache. Have been looking for something like this for a while. Really Worked for and saved lot of space on server.

    Thanks a lot for sharing such nice script.

    1. Thanks Viral, that’s no problem. I knew this helped me out loads and its clearly now helping others which was the main aim of uploading this. My next task is code this into a C# executable that offers much more flexibility. That’s for another day though.

  6. Absolutely brilliant. I also was an ICSweep user. This is spectacular, expecially now with Chrome on our 250user TS.

  7. Just saved me 9 GB of storage and who knows how many files on a terminal server. I spent 3 hours a few weeks ago manually deleting 40K files which netted me about 1 GB I believe. I’m anticipating the server running better tomorrow as a result of cleaning out all those small files.

    Awesome Job, Thanks!
    Best Regards,
    Glenn

    1. Thanks for letting me know Glenn. This was spawned from a similar effort cleaning out 000’s of files for not much gain.
      I’m really glad this is helping you.

      Regards

      Lee

  8. Hi,

    Great script! I’m also avoided the csv, and won’t use “get-childitems C:\Users” either, just went to a one-liner:
    Remove-Item C:\Users\*\AppData\Local\Google\Chrome

    I have to say IE has a corporate use (so I won’t delete anything there), Firefox is not installed in our TS, and Chrome is… well, not officially supported. So this one-liner fits better for us.

    Thanks for the idea, though! 😀

  9. Hi,

    Great Script, I cleaned up thousand of uncecessary files,
    However, I would like to have an output of this script to a log file instead of having this crazy verbose on screen..
    Anyone could help please?

    1. By piping Out-File “c:\mylogpath.txt” you can export the output to a file, for example,

      Get-ChildItem “c:\users” | Out-File “c:\mylogpath.txt”

      Sorry for the basic and short reply, but I’m out and about. Let me know if you get stuck and I’ll go into more detail.

      Thanks for your comment

      Regards

      1. Hi There,
        Thanks for your reply.
        Before asking you, I Did already try OUT-File to a txt file
        Txt file is created but file is empty,
        It does not contain the output however script has ran correctly.
        Any idea?

        Thank in advance

  10. Script is looking great Eredn. But i think it’s a bit too much for what i’m looking for.
    I’m currently looking for a logoff script on my customers environment to only clear (or delete) de webcache folder of IE in a neat way.
    In your script it looks like it only clears the temp history folder and not the webcache folder. Am i right?

    1. Hi there, sorry for the delay.
      You would just need to amend the script in the # Clear Internet Explorer section so that one of the paths is:
      “C:\Users\$($_.Name)\AppData\Local\Microsoft\Windows\WebCache\*”
      You could remove both the Mozilla and Chrome sections so that the script only runs your 1 removal instruction 🙂

  11. Thanks for the script. It’s been very useful for me. As usual there’s one sticking point on my end that I was hoping you would help with. Is there an easy way to pass over a specific user or file from being removed? “C:\Users\$($_.Name)\AppData\Local\Temp\*” has one folder in it from a single user that is called in an application that needs to stay running. Any help you can provide would be appreciated.

    Toby

    1. Hi Tobias, sorry for the delay. Yes, you can use the following line of code to replace the line that is affecting you:
      Get-ChildItem -Path 'C:\RootFolderPath\*' -Recurse | Where-Object {($_.FullName -notlike "C:\RootFolderPath\FolderToExclude*") } | Remove-Item -Recurse

      For Example, if you had a folder in the C:\Windows\Temp directory called Application1 and you wanted to keep the folder Application1….
      Get-ChildItem -Path 'C:\Windows\Temp\*' -Recurse | Where-Object {($_.FullName -notlike "C:\Windows\Temp\Application1*") } | Remove-Item -Recurse

  12. hi Erden that is great job, however if i need this script running by PC name and clean all users cash on each pc what can i do

    1. Hi there,
      Firstly, are your PCs domain joined?
      If they are, you could configure Group Policy to run this script at startup/shutdown? And use Item Level Targeting to only apply to a group of PCs that you specify 🙂

      Regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s