Skip to content

Instantly share code, notes, and snippets.

@MattRay0295
Created July 19, 2023 20:58
Show Gist options
  • Save MattRay0295/794a43384b69b17308e57e415f4063ec to your computer and use it in GitHub Desktop.
Save MattRay0295/794a43384b69b17308e57e415f4063ec to your computer and use it in GitHub Desktop.
Powershell Script Clear some Windows Caches / Chrome / Firefox
#------------------------------------------------------------------#
#- Clear-WindowsUserCacheFiles #
#------------------------------------------------------------------#
function Clear-WindowsUserCacheFiles {
param([string]$user = $env:USERNAME)
Remove-CacheFiles "C:\Users\$user\AppData\Local\Temp"
Remove-CacheFiles "C:\Users\$user\AppData\Local\Microsoft\Windows\WER"
Remove-CacheFiles "C:\Users\$user\AppData\Local\Microsoft\Windows\Temporary Internet Files"
}
#------------------------------------------------------------------#
#- Clear-GlobalWindowsCache #
#------------------------------------------------------------------#
function Clear-GlobalWindowsCache {
Remove-CacheFiles 'C:\Windows\Temp'
Remove-CacheFiles "C:\`$Recycle.Bin"
Remove-CacheFiles "C:\Windows\Prefetch"
C:\Windows\System32\rundll32.exe InetCpl.cpl,ClearMyTracksByProcess 255
C:\Windows\System32\rundll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351
}
#------------------------------------------------------------------#
#- Clear-ChromeCache #
#------------------------------------------------------------------#
function Clear-ChromeCache {
param([string]$user = $env:USERNAME)
if ((Test-Path "C:\users\$user\AppData\Local\Google\Chrome\User Data\Default"))
{
$chromeAppData = "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default"
# $possibleCachePaths = @('Cache','Cache2\entries\','Cookies','History','Top Sites','VisitedLinks','Web Data','Media Cache','Cookies-Journal','ChromeDWriteFontCache')
$possibleCachePaths = @('Cache','Cache2\entries\','Cookies','History','Top Sites','VisitedLinks','Media Cache','Cookies-Journal','ChromeDWriteFontCache')
foreach ($cachePath in $possibleCachePaths)
{
Remove-CacheFiles "$chromeAppData\$cachePath"
}
}
}
#------------------------------------------------------------------#
#- Clear-UserCacheFiles #
#------------------------------------------------------------------#
function Clear-UserCacheFiles {
Close-BrowserSessions
foreach ($localUser in (Get-ChildItem 'C:\users').Name)
{
Clear-ChromeCache $localUser
Clear-FirefoxCacheFiles $localUser
Clear-WindowsUserCacheFiles $localUser
}
}
#------------------------------------------------------------------#
#- Clear-FirefoxCacheFiles #
#------------------------------------------------------------------#
function Clear-FirefoxCacheFiles {
param([string]$user = $env:USERNAME)
if ((Test-Path "C:\users\$user\AppData\Local\Mozilla\Firefox\Profiles"))
{
$possibleCachePaths = @('cache','cache2\entries','thumbnails','cookies.sqlite','webappsstore.sqlite','chromeappstore.sqlite')
$firefoxAppDataPath = (Get-ChildItem "C:\users\$user\AppData\Local\Mozilla\Firefox\Profiles" | Where-Object { $_.Name -match 'Default' }[0]).FullName
foreach ($cachePath in $possibleCachePaths)
{
Remove-CacheFiles "$firefoxAppDataPath\$cachePath"
}
}
}
#------------------------------------------------------------------#
#- Clear-WaterfoxCacheFiles #
#------------------------------------------------------------------#
function Clear-WaterfoxCacheFiles {
param([string]$user = $env:USERNAME)
if ((Test-Path "C:\users\$user\AppData\Local\Waterfox\Profiles"))
{
$possibleCachePaths = @('cache','cache2\entries','thumbnails','cookies.sqlite','webappsstore.sqlite','chromeappstore.sqlite')
$waterfoxAppDataPath = (Get-ChildItem "C:\users\$user\AppData\Local\Waterfox\Profiles" | Where-Object { $_.Name -match 'Default' }[0]).FullName
foreach ($cachePath in $possibleCachePaths)
{
Remove-CacheFiles "$waterfoxAppDataPath\$cachePath"
}
}
}
#------------------------------------------------------------------#
#- Close-BrowserSessions #
#------------------------------------------------------------------#
function Close-BrowserSessions {
$activeBrowsers = Get-Process Firefox*,Chrome*,Waterfox*
foreach ($browserProcess in $activeBrowsers)
{
try
{
$browserProcess.CloseMainWindow() | Out-Null
} catch {}
}
}
#------------------------------------------------------------------#
#- Remove-CacheFiles #
#------------------------------------------------------------------#
function Remove-CacheFiles {
param([Parameter(Mandatory = $true)] [string]$path)
begin
{
$originalVerbosePreference = $VerbosePreference
$VerbosePreference = 'Continue'
}
process
{
if ((Test-Path $path))
{
if ([System.IO.Directory]::Exists($path))
{
try
{
if ($path[-1] -eq '\')
{
[int]$pathSubString = $path.ToCharArray().Count - 1
$sanitizedPath = $path.SubString(0,$pathSubString)
Remove-Item -Path "$sanitizedPath\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
}
else
{
Remove-Item -Path "$path\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose
}
} catch {}
}
else
{
try
{
Remove-Item -Path $path -Force -ErrorAction SilentlyContinue -Verbose
} catch {}
}
}
}
end
{
$VerbosePreference = $originalVerbosePreference
}
}
#------------------------------------------------------------------#
#- MAIN #
#------------------------------------------------------------------#
Clear-UserCacheFiles
Clear-GlobalWindowsCache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment