CleanRoamingProfile.vbs

Roaming profiles can cause slow logon and logoff times. Over time each users roaming profile will increase in size until it becomes so large and/or contains so many files that logon times will become noticably affected [Loading your Personal Settings...]

This script will delete selected files from the user profile of the account currently logged on. Typically it would be run as part of a logon script. The script normally runs in a fraction of a second, it displays the elasped time so you can see exactly how long it takes.

Rather than clearing out everything, this script deletes files based on their last modified date.

By default it is set to delete cookies over 90 days old, 'Recent document' shortcuts over 14 days old and cached Internet Explorer, Flash, Oracle & Java files over 5 days old. You can of course edit these settings to suit your environment.

This script will delete files - make a backup before running it on any live system.

For compatibility with Windows 7 the script uses the %AppData% variable rather than hard coding "Application Data"

Option Explicit

'Variables
Dim objShell,FSO,dtmStart,dtmEnd
Dim strUserProfile,strAppData
Dim objFolder,objFile

Wscript.echo "Profile cleanup starting"
dtmStart = Timer()

' Get the current users Profile and ApplicationData folders
Set objShell = CreateObject("WScript.Shell")
strUserProfile=objShell.ExpandEnvironmentStrings("%USERPROFILE%")
strAppData=objShell.ExpandEnvironmentStrings("%APPDATA%")
'Wscript.echo strAppData

' Set reference to the file system
Set FSO = createobject("Scripting.FileSystemObject")

' Call the DeleteOlder subroutine for each folder

DeleteOlder 90, strUserProfile & "\Cookies"  'Days to keep cookies
DeleteOlder 14, strUserProfile & "\Recent"   'Days to keep recent files
DeleteOlder 14, strAppData & "\Microsoft\Office\Recent" 'Days to keep recent MS Office files
DeleteOlder 5, strAppData & "\Microsoft\CryptnetUrlCache\Content"  'IE certificate cache
DeleteOlder 5, strAppData & "\Microsoft\CryptnetUrlCache\MetaData" 'IE cert info
DeleteOlder 5, strAppData & "\Sun\Java\Deployment\cache" 'Days to keep Java cache
DeleteOlder 3, strAppData & "\Macromedia\Flash Player"   'Days to keep flash data
DeleteOlder 14, strUserProfile & "\Oracle Jar Cache"     'Days to keep Oracle Jar Cache

' Print completed message

dtmEnd = Timer()
Wscript.echo "Profile cleanup complete, elapsed time: " & FormatNumber(dtmEnd-dtmStart,2) & " seconds"
Set FSO = Nothing

' Subroutines below

Sub DeleteOlder(intDays,strPath)
' Delete files from strPath that are more than intDays old
If FSO.FolderExists(strPath) = True Then
   Set objFolder = FSO.GetFolder(strPath)
   For each objFile in objFolder.files
      If DateDiff("d", objFile.DateLastModified,Now) > intDays Then
         'Wscript.echo "File: " & objFile.Name
         objFile.Delete(True)
      End If
   Next
   Set objFile = Nothing
   Set objFolder = Nothing
End If
End Sub

Run this script using cscript like this:

C:\> cscript CleanRoamingProfile.vbs //nologo

The performance benefits of reducing the profile folder size may not appear until the second time the script is run. For example for a user with 1000 recent document shortcuts the script might parse through 1000 files, keep the most recent 200 and delete the rest. That process will likely be slower than just copying all 1000 files. However the next time the user logs on they will see a faster logon with fewer files needing to be processed.

Copying a large number of small files can take a disproportionately long time due to disc cluster sizes and SMB limitations. This is why Temporary Internet Files are excluded from roaming profiles by default.

On a typical user profile the script should run to completion in less than a quarter of a second.

“Let him that would move the world, first move himself” - Socrates

Related:

Q319909 - Slow Logoff process due to an open registry handle
Q325376 - Enable verbose logon and logoff status messages
Troubleshooting slow logons - NedPyle
Explorer - Slow Network Browsing
PROQUOTA - Limit user profile size
VM Ware View - Virtualised desktops + virtual Profiles



Back to the Top

© Copyright SS64.com 1999-2012
Some rights reserved