-
-
Save Cdaprod/a2f70a781ea13c0e2199e2ee8cb72e05 to your computer and use it in GitHub Desktop.
Simple script to clear temp files and browser cache/history
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------# | |
#- 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-UserCacheFiles # | |
#------------------------------------------------------------------# | |
Function Clear-UserCacheFiles { | |
# Stop-BrowserSessions | |
ForEach($localUser in (Get-ChildItem 'C:\users').Name) | |
{ | |
Clear-ChromeCache $localUser | |
Clear-EdgeCacheFiles $localUser | |
Clear-FirefoxCacheFiles $localUser | |
Clear-WindowsUserCacheFiles $localUser | |
Clear-TeamsCacheFiles $localUser | |
} | |
} | |
#------------------------------------------------------------------# | |
#- 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\INetCache" | |
Remove-CacheFiles "C:\Users\$user\AppData\Local\Microsoft\Windows\INetCookies" | |
Remove-CacheFiles "C:\Users\$user\AppData\Local\Microsoft\Windows\IECompatCache" | |
Remove-CacheFiles "C:\Users\$user\AppData\Local\Microsoft\Windows\IECompatUaCache" | |
Remove-CacheFiles "C:\Users\$user\AppData\Local\Microsoft\Windows\IEDownloadHistory" | |
Remove-CacheFiles "C:\Users\$user\AppData\Local\Microsoft\Windows\Temporary Internet Files" | |
} | |
#Region HelperFunctions | |
#------------------------------------------------------------------# | |
#- Stop-BrowserSessions # | |
#------------------------------------------------------------------# | |
Function Stop-BrowserSessions { | |
$activeBrowsers = Get-Process Firefox*,Chrome*,Waterfox*,Edge* | |
ForEach($browserProcess in $activeBrowsers) | |
{ | |
try | |
{ | |
$browserProcess.CloseMainWindow() | Out-Null | |
} catch { } | |
} | |
} | |
#------------------------------------------------------------------# | |
#- Get-StorageSize # | |
#------------------------------------------------------------------# | |
Function Get-StorageSize { | |
Get-WmiObject Win32_LogicalDisk | | |
Where-Object { $_.DriveType -eq "3" } | | |
Select-Object SystemName, | |
@{ Name = "Drive" ; Expression = { ( $_.DeviceID ) } }, | |
@{ Name = "Size (GB)" ; Expression = {"{0:N1}" -f ( $_.Size / 1gb)}}, | |
@{ Name = "FreeSpace (GB)" ; Expression = {"{0:N1}" -f ( $_.Freespace / 1gb ) } }, | |
@{ Name = "PercentFree" ; Expression = {"{0:P1}" -f ( $_.FreeSpace / $_.Size ) } } | | |
Format-Table -AutoSize | Out-String | |
} | |
#------------------------------------------------------------------# | |
#- 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 | |
} | |
} | |
#Endregion HelperFunctions | |
#Region Browsers | |
#Region ChromiumBrowsers | |
#------------------------------------------------------------------# | |
#- 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') | |
ForEach($cachePath in $possibleCachePaths) | |
{ | |
Remove-CacheFiles "$chromeAppData\$cachePath" | |
} | |
} | |
} | |
#------------------------------------------------------------------# | |
#- Clear-EdgeCache # | |
#------------------------------------------------------------------# | |
Function Clear-EdgeCache { | |
param([string]$user=$env:USERNAME) | |
if((Test-Path "C:\Users$user\AppData\Local\Microsoft\Edge\User Data\Default")) | |
{ | |
$EdgeAppData = "C:\Users$user\AppData\Local\Microsoft\Edge\User Data\Default" | |
$possibleCachePaths = @('Cache','Cache2\entries','Cookies','History','Top Sites','Visited Links','Web Data','Media History','Cookies-Journal') | |
ForEach($cachePath in $possibleCachePaths) | |
{ | |
Remove-CacheFiles "$EdgeAppData$cachePath" | |
} | |
} | |
} | |
#Endregion ChromiumBrowsers | |
#Region FirefoxBrowsers | |
#------------------------------------------------------------------# | |
#- 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" | |
} | |
} | |
} | |
#Endregion FirefoxBrowsers | |
#Endregion Browsers | |
#Region CommunicationPlatforms | |
#------------------------------------------------------------------# | |
#- Clear-TeamsCacheFiles # | |
#------------------------------------------------------------------# | |
Function Clear-TeamsCacheFiles { | |
param([string]$user=$env:USERNAME) | |
if((Test-Path "C:\users\$user\AppData\Roaming\Microsoft\Teams")) | |
{ | |
$possibleCachePaths = @('cache','blob_storage','databases','gpucache','Indexeddb','Local Storage','application cache\cache') | |
$teamsAppDataPath = (Get-ChildItem "C:\users\$user\AppData\Roaming\Microsoft\Teams" | Where-Object { $_.Name -match 'Default' }[0]).FullName | |
ForEach($cachePath in $possibleCachePaths) | |
{ | |
Remove-CacheFiles "$teamsAppDataPath\$cachePath" | |
} | |
} | |
} | |
#Endregion CommunicationPlatforms | |
#------------------------------------------------------------------# | |
#- MAIN # | |
#------------------------------------------------------------------# | |
$StartTime = (Get-Date) | |
Get-StorageSize | |
Clear-UserCacheFiles | |
Clear-GlobalWindowsCache | |
Get-StorageSize | |
$EndTime = (Get-Date) | |
Write-Verbose "Elapsed Time: $(($StartTime - $EndTime).totalseconds) seconds" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment