Advertisement
smartguy5000

clearCache.ps1

Apr 26th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.     Powershell script to automate the clearing of IE and Chrome caches for all users on a machine.
  4. .DESCRIPTION
  5.     Programmatically clears out the IE cache with a builtin run dll function, and clears out the history registry key, and clears out the folder where
  6.     chrome stores all of it's user data, and then restarts the computer
  7. .PARAMETER
  8. .EXAMPLE
  9.     .\clearCache.ps1
  10. .INPUTS
  11. .OUTPUTS
  12. .NOTES
  13.     Name: clearCache.ps1
  14.     Author: Smartguy5000
  15.     Last Modified: 04/06/2016 15:14
  16. .CHANGELOG
  17. v1 - initial write
  18. v1.1 - prevented deletion of bookmarks for chrome users
  19. v1.2 - fixed stupid remove-item cmdlet
  20. .VERSION
  21. 1.1
  22.  
  23. .LINK
  24. #>
  25. [CmdletBinding()]
  26. Param(
  27. )
  28. #Remove IE cookies and Cache etc.
  29. $pref = $VerbosePreference
  30. $VerbosePreference = 'Continue'
  31. $proc = Get-Process Iexplore -ErrorAction 'SilentlyContinue'
  32. If ($proc -ne $null)
  33.     {        
  34.             Get-Process iexplore | Stop-Process -Force      
  35.     }
  36.  
  37. $proc = Get-Process chrome -ErrorAction 'SilentlyContinue'
  38. If ($proc -ne $null)
  39.     {        
  40.             Get-Process chrome | Stop-Process -Force      
  41.     }
  42.  
  43.  
  44. if (Test-Path -Path $env:SystemRoot\System32\RunDll32.exe)
  45.  
  46.     {
  47.         Start-Process -FilePath $env:SystemRoot\System32\RunDll32.exe -ArgumentList 'InetCpl.cpl, ClearMyTracksByProcess 255'
  48.     }
  49. else
  50.  
  51.     {
  52.         Throw "RunDll32.exe is not present on this device. Please repair windows before running this script again"
  53.     }
  54.  
  55.  
  56. #remove chrome cache and cookies etc.
  57.  
  58.  
  59. $aUsers = Get-ChildItem "C:\Users" -Exclude Public
  60. if (!($null -eq $aUsers))
  61.    
  62.     {
  63.         ForEach ($user in $aUsers)
  64.            
  65.             {Write-Verbose $user
  66.                 if (Test-Path "$user\AppData\Local\Google\Chrome\User Data\*")
  67.                    
  68.                     {
  69.                     $chromePath = "$user\AppData\Local\Google\Chrome\User Data\"
  70.                         Try
  71.                             {                                                              
  72.                                 Get-ChildItem -file "$chromePath*" | Remove-Item -Force -recurse
  73.                                 $arrayofdirectories = Get-ChildItem -Directory "$chromePath*" | Where-Object {$_.Name -ne "Default"}
  74.                                 ForEach ($dir in $arrayofdirectories)
  75.                                     {
  76.                                         Get-ChildItem -File $Dir -Recurse | Remove-Item -Force
  77.                                         Remove-Item $Dir
  78.                                     }
  79.                                 Get-ChildItem -File "${chromePath}Default\*" | Where-Object {$_.Name -ne "Bookmarks"} | Remove-Item -Force -recurse
  80.                                 $ArrayOfDirectories = Get-ChildItem -Directory "${chromePath}Default\*"
  81.                                 ForEach ($Dir in $ArrayOfDirectories)
  82.                                     {
  83.                                         Get-ChildItem -File $Dir -Recurse | Remove-Item -force
  84.                                         Remove-Item $Dir -force -recurse
  85.                                     }                            
  86.                             }
  87.                         Catch [System.UnauthorizedAccessException]
  88.                             {
  89.                                 Throw "Insuficcient rights to access this directory"
  90.                             }
  91.                        
  92.                        
  93.                     }
  94.             }
  95.     }
  96. else
  97.  
  98.     {
  99.         Throw "There are no user profiles in the directory C:\Users"
  100.     }
  101.  
  102.            
  103.                    
  104.    
  105.  
  106. #clean up after yourself and reboot
  107. Remove-Item (Get-Item $myInvocation.myCommand.path) -force -recurse
  108.  
  109. if (Test-Path -Path $env:SystemRoot\System32\Shutdown.exe)
  110.  
  111.     {
  112.         Start-Process -FilePath $env:SystemRoot\System32\Shutdown.exe -ArgumentList '-r -t 15 -c "Computer will restart in 15 seconds due to scheduled maintenance"'
  113.     }
  114. else
  115.  
  116.     {
  117.         Throw "Shutdown.exe is not present on this device. Please repair windows before running this script again"
  118.     }
  119. $VerbosePreference = $pref
  120. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement