Month: August 2011

Script to delete specific IE cookie

Posted on Updated on

We had a recent need to delete IE cookies from a specific website throughout the organization.

'==========================================================================
' NAME: DeleteIeCookies
' AUTHOR: Nick Moseley , https://t3chn1ck.wordpress.com
' VERSION: 
' 1.0 (08/11/2011) - Initial script
' 2.0 (11/10/2011) - Updated to support Win7 vs. WinXP
' COMMENT: Deletes the cookies specified in variable CookieName.  To use, 
' set variable "CookieName" to what you need to delete.
'==========================================================================
Option Explicit

Const CookieName = "yahoo"
Const ForAppending = 8
Const HKLM = &H80000002
Const EnableLogging = True
Const LogFile = "C:\DeleteIeCookies.log"

' Basic objects
Dim oReg, oShell, oFSO, iCountFiles
Set oReg = GetObject("winmgmts:\\.\root\default:StdRegProv")
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject ("Scripting.FileSystemObject")
iCountFiles = 0

' Get user folder location for Vista/7 vs. XP
Dim sOS
If oFSO.FolderExists ("C:\Users") Then
 sOS = "win7"
Else
 sOS = "xp"
End If


'==========================================================================
' Begin log file etnries
'==========================================================================
If EnableLogging = True Then 
 Dim oVersionLog
 If Not oFSO.FileExists(LogFile) Then
  oFSO.CreateTextFile LogFile
 End If
 Set oVersionLog = oFSO.OpenTextFile (LogFile, ForAppending, True)
 
 oVersionLog.WriteLine "Delete Craigslist cookies"
 oVersionLog.WriteLine "  => Began at " & Date & " " & Time
End If
'==========================================================================

' Get list of folders in C:\Users
Dim oUserFolders, oFolder, colFiles, oUserCookieFolder, oFile
If sOS = "win7" Then
 Set oUserFolders = oFSO.GetFolder("C:\Users") 
Else
 Set oUserFolders = oFSO.GetFolder("C:\Documents and Settings") 
End If

For Each oFolder In oUserFolders.subfolders 
 If sOS = "win7" Then
  DeleteFile ("C:\Users\" & oFolder.name & "\AppData\Roaming\Microsoft\Windows\Cookies") 
  DeleteFile ("C:\Users\" & oFolder.name & "\AppData\Roaming\Microsoft\Windows\Cookies\Low") 
 Else
  DeleteFile ("C:\Documents and Settings\" & oFolder.name & "\Cookies") 
  DeleteFile ("C:\Documents and Settings\" & oFolder.name & "\Local Settings\Temporary Internet Files") 
 End If
Next

' End logging
If EnableLogging = True Then 
 oVersionLog.WriteLine "  => Total number of deleted cookies: " & iCountFiles
 oVersionLog.WriteLine "  => Completed at " & Date & " " & Time
 oVersionLog.Close
End If

WScript.Quit

Sub DeleteFile (sFolderPath)
 If oFSO.FolderExists (sFolderPath) Then
  Set oUserCookieFolder = oFSO.GetFolder(sFolderPath)
  Set colFiles = oUserCookieFolder.Files
  For Each oFile In colFiles
   If InStr (oFile.Name, CookieName) Then 
    'WScript.Echo "file: " & oFile.Name
    If EnableLogging = True Then
     oVersionLog.WriteLine "  => Deleted " & sFolderPath & "\" & oFile.Name
    Else
     'WScript.Echo sFolderPath & "\" & oFile.Name
    End If
    oFSO.DeleteFile sFolderPath & "\" & oFile.Name
    iCountFiles = iCountFiles + 1
   End If
  Next 
 End If 
End Sub