This VBScript could be run as a start-up or shutdown script of windows GPO or with a scheduled task must be run as administrative user to Clear Local User Chache Files and System Registry Entries of old or orphan user profiles.
Tested on the following Microsoft OS versions:
- Microsoft Windows XP (32bit)
- Microsoft Windows 7 (32bit)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
' ############################################################################## ' # Lupi Gabriel # ' # www.lupigabriel.it # ' # info@lupigabriel.it # ' ############################################################################## ' # Copyright (c) 2014 Lupi Gabriel All rights reserved. # ' ############################################################################## ' # VERSION : 1.0 # ' # FILE : ClearCachedProfile.vbs # ' # CLASS : Script # ' # LANGUAGE : VB Script # ' # LOG FILE : "%SystemRoot%\ClearCachedProfile.log" # ' ############################################################################## On Error Resume Next ClearNormalMonths = 12 ' #### CLEAR USER PROFILE CACHE AFTER * MONTHS ClearMandatoryWeeks = 1 ' #### CLEAR MANDATORY USER PROFILE CACHE AFTER * WEEKS Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv") Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") Set objFSO = CreateObject("Scripting.FileSystemObject") Set wshShell = CreateObject("WScript.Shell") LogFilePath = wshShell.ExpandEnvironmentStrings("%SystemRoot%") & "\ClearCachedProfile.log" If objFSO.FileExists(LogFilePath) = True Then Set f = objFSO.GetFile(LogFilePath) If DateDiff("m",f.DateLastModified,Now()) > 1 Then objFSO.DeleteFile LogFilePath End If End If Set objFile = objFSO.OpenTextFile(LogFilePath, 8, True) if objRegistry.EnumKey(&H80000002, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", valueNames1) = 0 then if isArray(valueNames1) then for i1 = 0 to UBound(valueNames1) LocalNTuser = "" LocalSID = valueNames1(i1) objRegistry.GetStringValue &H80000002, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & LocalSID, "Guid", GuidValue objRegistry.GetStringValue &H80000002, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & LocalSID, "ProfileImagePath", PathValue If InStr(LocalSID,"S-1-5-21-137065001-") > 0 Then Set objAccount = objWMIService.Get("Win32_SID.SID='" & Split(LocalSID,".",-1,1)(0) & "'") IsMandatory = "" If objFSO.FileExists(Trim(PathValue) & "\ntuser.man") = True Then LocalNTuser = Trim(PathValue) & "\ntuser.man" IsMandatory = "ON" End If If objFSO.FileExists(Trim(PathValue) & "\ntuser.dat") = True Then LocalNTuser = Trim(PathValue) & "\ntuser.dat" IsMandatory = "OFF" End If SpuriousProfile = "No" If objFSO.FolderExists(Trim(PathValue)) = False Then SpuriousProfile = "Yes" End If LastLogonW = 0 LastLogonM = 0 If IsMandatory <> "" Then Set f = objFSO.GetFile(LocalNTuser) LastLogonW = DateDiff("w",f.DateLastModified,Now()) LastLogonM = DateDiff("m",f.DateLastModified,Now()) Set f = Nothing End If ClearProfile = 0 If IsMandatory = "ON" And LastLogonW > ClearMandatoryWeeks Then ClearProfile = 1 End If If IsMandatory = "OFF" And LastLogonM > ClearNormalMonths Then ClearProfile = 1 End If If SpuriousProfile = "Yes" Then ClearProfile = 1 End If Wscript.Echo Wscript.Echo "Path = " & Trim(PathValue) Wscript.Echo "SID = " & LocalSID Wscript.Echo "GUID = " & GuidValue Wscript.Echo "Mandatory = " & IsMandatory Wscript.Echo "LastLogon = " & LastLogonW & " Weeks - " & LastLogonM & " Months" Wscript.Echo "Spurious Profile = " & SpuriousProfile Wscript.Echo "IS DELETABLE = " & ClearProfile If ClearProfile = 1 Then objRegistry.DeleteKey &H80000002, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & LocalSID & "\Preference" objRegistry.DeleteKey &H80000002, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & LocalSID objRegistry.DeleteKey &H80000002, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileGuid\" & GuidValue objRegistry.DeleteKey &H80000002, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\PolicyGuid\" & GuidValue If objFSO.FolderExists(Trim(PathValue)) = True Then wshShell.run "ccp.bat """ & Trim(PathValue) & "\""",1,1 End If End If If ClearProfile = 1 Then objFile.WriteLine "" objFile.WriteLine "" objFile.WriteLine "" objFile.WriteLine Now() objFile.WriteLine "" objFile.WriteLine "Path = " & Trim(PathValue) objFile.WriteLine "SID = " & LocalSID objFile.WriteLine "GUID = " & GuidValue objFile.WriteLine "Mandatory = " & IsMandatory objFile.WriteLine "LastLogon = " & LastLogonW & " Weeks - " & LastLogonM & " Months" objFile.WriteLine "Spurious Profile = " & SpuriousProfile objFile.WriteLine "" objFile.WriteLine "==================================================" End If End If Set objAccount = Nothing next end if end if objFile.Close Set objAccount = Nothing Set objRegistry = Nothing Set objWMIService = Nothing Set objFSO = Nothing Set wshShell = Nothing Set objFile = Nothing Set f = Nothing 'end script ClearCachedProfile |