Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Powershell script to automate the clearing of IE and Chrome caches for all users on a machine.
- .DESCRIPTION
- 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
- chrome stores all of it's user data, and then restarts the computer
- .PARAMETER
- .EXAMPLE
- .\clearCache.ps1
- .INPUTS
- .OUTPUTS
- .NOTES
- Name: clearCache.ps1
- Author: Smartguy5000
- Last Modified: 04/06/2016 15:14
- .CHANGELOG
- v1 - initial write
- v1.1 - prevented deletion of bookmarks for chrome users
- v1.2 - fixed stupid remove-item cmdlet
- .VERSION
- 1.1
- .LINK
- #>
- [CmdletBinding()]
- Param(
- )
- #Remove IE cookies and Cache etc.
- $pref = $VerbosePreference
- $VerbosePreference = 'Continue'
- $proc = Get-Process Iexplore -ErrorAction 'SilentlyContinue'
- If ($proc -ne $null)
- {
- Get-Process iexplore | Stop-Process -Force
- }
- $proc = Get-Process chrome -ErrorAction 'SilentlyContinue'
- If ($proc -ne $null)
- {
- Get-Process chrome | Stop-Process -Force
- }
- if (Test-Path -Path $env:SystemRoot\System32\RunDll32.exe)
- {
- Start-Process -FilePath $env:SystemRoot\System32\RunDll32.exe -ArgumentList 'InetCpl.cpl, ClearMyTracksByProcess 255'
- }
- else
- {
- Throw "RunDll32.exe is not present on this device. Please repair windows before running this script again"
- }
- #remove chrome cache and cookies etc.
- $aUsers = Get-ChildItem "C:\Users" -Exclude Public
- if (!($null -eq $aUsers))
- {
- ForEach ($user in $aUsers)
- {Write-Verbose $user
- if (Test-Path "$user\AppData\Local\Google\Chrome\User Data\*")
- {
- $chromePath = "$user\AppData\Local\Google\Chrome\User Data\"
- Try
- {
- Get-ChildItem -file "$chromePath*" | Remove-Item -Force -recurse
- $arrayofdirectories = Get-ChildItem -Directory "$chromePath*" | Where-Object {$_.Name -ne "Default"}
- ForEach ($dir in $arrayofdirectories)
- {
- Get-ChildItem -File $Dir -Recurse | Remove-Item -Force
- Remove-Item $Dir
- }
- Get-ChildItem -File "${chromePath}Default\*" | Where-Object {$_.Name -ne "Bookmarks"} | Remove-Item -Force -recurse
- $ArrayOfDirectories = Get-ChildItem -Directory "${chromePath}Default\*"
- ForEach ($Dir in $ArrayOfDirectories)
- {
- Get-ChildItem -File $Dir -Recurse | Remove-Item -force
- Remove-Item $Dir -force -recurse
- }
- }
- Catch [System.UnauthorizedAccessException]
- {
- Throw "Insuficcient rights to access this directory"
- }
- }
- }
- }
- else
- {
- Throw "There are no user profiles in the directory C:\Users"
- }
- #clean up after yourself and reboot
- Remove-Item (Get-Item $myInvocation.myCommand.path) -force -recurse
- if (Test-Path -Path $env:SystemRoot\System32\Shutdown.exe)
- {
- Start-Process -FilePath $env:SystemRoot\System32\Shutdown.exe -ArgumentList '-r -t 15 -c "Computer will restart in 15 seconds due to scheduled maintenance"'
- }
- else
- {
- Throw "Shutdown.exe is not present on this device. Please repair windows before running this script again"
- }
- $VerbosePreference = $pref
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement