Clear Browser Cache all users

from: https://liamarjitbhogal.wordpress.com/2014/08/13/powershell-script-to-reset-browser-caches-for-every-user-on-a-terminal-server-google-chrome-mozilla-ien/

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 – works locally as well. It takes around 30 seconds to complete (user # dependant).

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.

This saves you hours of time trawling user data for tit-bits of space.

Before you hit play, you must use “dsquery” to get the list of SAMIDs for your users and this needs to be run on a DC!

Here goes (it may need a little tweaking for your specific environment):

Clear_cache_allusers.ps1
#######################################################
#Powershell commands to delete cache & cookies in Firefox, Chrome & IE browsers
#By Lee Bhogal, Paradise Computing Ltd – June 2014
#######################################################”
 
#Use this section to get the list of users. (Run on a DC, remove the #s):
#$ou = Read-Host “Please type the Users OU in the following format ‘OU=Users,OU=CompanyOU,DC=CompanyDomain,DC=com'”
#dsquery user “$ou” -limit 1000 | dsget user -samid > C:\users.csv
 
“Copy C:\users.csv from the DC to the root of the TS”
 
$CSVPath=Read-Host “Please enter the full path to the CSV containing the list of users without quotation, ensure title of column A is SAMID and the entries contain no spaces before the usernames”
“——————-“
 
#Clear Mozilla Firefox Cache
“Clearing Mozilla Firefox Caches”
 
$ListofUsers=Get-Content$CSVPathforeach ($SAMID in $ListofUsers)
{
Remove-Item C:\Users\$SAMID\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache\* -Recurse -Force -EA SilentlyContinue
Remove-Item C:\Users\$SAMID\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache\*.* -Recurse -Force -EA SilentlyContinue
Remove-Item C:\Users\$SAMID\AppData\Local\Mozilla\Firefox\Profiles\*.default\thumbnails\* -Recurse -Force -EA SilentlyContinue
Remove-Item C:\Users\$SAMID\AppData\Local\Mozilla\Firefox\Profiles\*.default\cookies.sqlite -Recurse -Force -EA SilentlyContinue
Remove-Item C:\Users\$SAMID\AppData\Local\Mozilla\Firefox\Profiles\*.default\webappsstore.sqlite -Recurse -Force -EA SilentlyContinue
Remove-Item C:\Users\$SAMID\AppData\Local\Mozilla\Firefox\Profiles\*.default\chromeappsstore.sqlite -Recurse -Force -EA SilentlyContinue
}
 
“——————-“
 
#Clear Google Chrome 
“Clearing Google Chrome Caches”
 
foreach ($SAMID in $ListofUsers)
{
Remove-Item “C:\Users\$SAMID\AppData\Local\Google\Chrome\User Data\Default\Cache\*-Recurse -Force -EA SilentlyContinue
Remove-Item “C:\Users\$SAMID\AppData\Local\Google\Chrome\User Data\Default\Cookies” -Recurse -Force -EA SilentlyContinue
Remove-Item “C:\Users\$SAMID\AppData\Local\Google\Chrome\User Data\Default\Media Cache” -Recurse -Force -EA SilentlyContinue
Remove-Item “C:\Users\$SAMID\AppData\Local\Google\Chrome\User Data\Default\Cookies-Journal” -Recurse -Force -EA SilentlyContinue
}
 
“——————-“
 
#Clear Internet Explorer
“Clearing Internet Explorer Caches”
 
foreach ($SAMID in $ListofUsers)
{
Remove-Item “C:\Users\$SAMID\AppData\Local\Microsoft\Windows\Temporary Internet Files” -Recurse -Force -EA SilentlyContinue
}
 
clear-host