How-to: 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 & Java files over 5 days old. You can of course edit the settings to suit your environment.

For compatibility with both new and old versions of Windows the script detects the OS and selects the appropriate User Profile folders.

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

Option Explicit

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

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")

'Get the windows version
strOSversion = objShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion")
'Wscript.echo strOSversion
'Call the DeleteOlder subroutine for each folder

'Application temp 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

'OS specific temp files
if Cint(Left(strOSversion,1)) > 5 Then
   Wscript.echo "Windows Vista/7/2008..."
   DeleteOlder 90, strAppData & "\Microsoft\Windows\Cookies"  'Days to keep cookies
   DeleteOlder 14, strAppData & "\Microsoft\Windows\Recent"   'Days to keep recent files
Else
   Wscript.echo "Windows 2000/2003/XP..."
   DeleteOlder 90, strUserProfile & "\Cookies"  'Days to keep cookies
   DeleteOlder 14, strUserProfile & "\Recent"   'Days to keep recent files
End if

'Print completed message

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

'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
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 VBScript commands

ProfileFolders - Location of User profile folders.
Q325376 - Enable verbose logon and logoff status messages.
Troubleshooting slow logons - Ned Pyle.
Explorer - Slow Network Browsing.


 
Copyright © 1999-2024 SS64.com
Some rights reserved