Script to delete Chrome cache on all PC’s listed in a text file

Another problem that could be fixed with a script today. Moving machines and users to a different domain resulted in a problem for some people. Chrome is a very popular browser but a number of people have reported to support that Chrome takes a very long time to start and then open the first page after log in to windows.

It was determined that deleting the cache directory from within the users Chrome application data folder resolved this.

Waiting for every effected user to report the problem wouldn’t be a great course of action of course. It would be a lot better to proactively go after all Chrome installations and remove the cache directory.

So that’s what I’ve done. The script does the following:

  1. Check to see if the computer is on the network by pinging it.
  2. List all the user directories in c:\users on each computer.
  3. Check to see if the Chrome cache directory exists.
  4. Deletes the cache directory.
  5. Logs the result including any encountered errors to a file called results.txt.

I could have searched Active Directory directly of course but I want to be able to easily add and remove computers when needed. It also allows me to run the script on 10 computers to start off and ramp up to a few thousand by the end.

It should go without saying that as the script uses the admin share, you need to have access to the workstations that you intend to administer.

Option Explicit
Const ForReading = 1
Const ForAppending = 8
Dim objFSO : Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Dim objFile : Set objFile = objFSO.OpenTextFile(“Z:\Scripts\Chrome cleanup\AccountingsectionComputers.txt”, ForReading, False)
Dim objWriteFile : Set objWriteFile = objFSO.OpenTextFile(“Z:\Scripts\Chrome cleanup\Result.txt”, ForAppending)

Do Until objFile.AtEndOfStream
Dim strComputer : strComputer = Trim(objFile.ReadLine)
If PingMachine(strComputer) Then
‘Computer is pinging
Dim objFolder : Set objFolder = objFSO.GetFolder(“\\” & strComputer & “\c$\users\”)
Dim strUserParentPath : Set strUserParentPath = objFolder.SubFolders
Dim strUserFolder
For Each strUserFolder in strUserParentPath
Dim strRemoteBasePath : strRemoteBasePath = “\\” & strComputer & “\c$\users\” & strUserFolder.name & “\AppData\Local\Google\Chrome\User Data\Default\Cache”
If objFSO.FolderExists(strRemoteBasePath) Then
On Error Resume Next
objFSO.DeleteFolder strRemoteBasePath, True
If Err.Number = 0 Then
objWriteFile.WriteLine strRemoteBasePath & vbTab & ” DELETE SUCCESSFUL”
Else
objWriteFile.WriteLine strRemoteBasePath & vbTab & ” DELETE FAILED: ” & Err.Number & ” ” & Err.Description
End If
On Error GoTo 0
Else
objWriteFile.WriteLine strRemoteBasePath & vbTab & ” FOLDER DOES NOT EXIST”
End If
Next
Else
objWriteFile.WriteLine strComputer & vbTab & ” PING FAILED”
End If
Loop
objFile.Close

Function PingMachine(device_name)
PingMachine = False
Dim colItems : Set colItems = GetObject(“winmgmts:root\cimv2”).ExecQuery _
(“Select StatusCode from Win32_PingStatus Where Address = ‘” & device_name & “‘”)
Dim objItem
For Each objItem in colItems
If objItem.StatusCode = 0 Then PingMachine = True
Next
End Function

If there are other scripts that you would like to see, please let me know in the comments section.