Host Africa

Tuesday 22 April 2014

VBS Script to clean all users profile (XP - Win8)

'
' Clean All Temp Etc.
'
' version 3.75
'
' by Jonathan Brickman, a substantial rewrite and
' enhancement of a core originally written by David Barrett
'
' last mod 2014-02-17
' Added clearing of profile WER data
' Added clearing of profile localappdata temp folders
' Fixed a few visual buglets
' Added clearing of Windows Error Reporting queue
' Added explicit search for system profiles
' Added clearing of Google Chrome cache
' Improved type setting for free space reports
' Added report as to how much space freed
' Added usability as SYSTEM user under Windows 7 et al.
' Added item ticks (comma for files, slash for subfolders)
'
' Core originally written by David Barrett
' Copyright 2009
' http://www.cedit.biz/
' This script is licensed under the Creative Commons
' Attribution 2.5 Licence
' http://creativecommons.org/licenses/by/2.5/
'
' You are free to use it for both personal and
' commercial purposes, so long as full attribution
' is given to the author (David Barrett).
'
' This notice must not be removed
'
' Substantially Enhanced by Jonathan E. Brickman
'
'

force_cscript

dim objWSH, objFSO, sysDrv
dim initialFreeSpace
dim strComputer
dim objRegistry
dim strKeyPath, objSubKey, arrSubkeys, strValueName
dim sProfile
dim sTemp, sTmp, sWindows, sLocalAppData, sProgramData, sWER, sDotNetItem

wscript.echo "CleanAllTemp"
wscript.echo ""
wscript.echo "originally by David Barrett"
wscript.echo "updated and modded substantially by Jonathan E. Brickman"
wscript.echo ""
wscript.echo "Cleans all profiles' temp and IE cache folders"
wscript.echo "A whole subfolder deletion is indicated by \"
wscript.echo "A file deletion is indicated by ,"
wscript.echo ""

set objFSO = CreateObject("Scripting.FileSystemObject")
set objWSH = CreateObject("WScript.Shell")
set sysDrv = objFSO.GetDrive(objFSO.GetDriveName(objWSH.ExpandEnvironmentStrings("%SystemDrive%")))

sTemp = objWSH.ExpandEnvironmentStrings("%TEMP%")
sTmp = objWSH.ExpandEnvironmentStrings("%TMP%")
sWindows = objWSH.ExpandEnvironmentStrings("%SystemRoot%")
sLocalAppData = objWSH.ExpandEnvironmentStrings("%LOCALAPPDATA%")
sProgramData = objWSH.ExpandEnvironmentStrings("%ProgramData%")
If sProgramData <> "" Then
sWER = objWSH.ExpandEnvironmentStrings("%ProgramData%") & "\Microsoft\Windows\WER\ReportQueue"
Else
sWER = ""
End If

' Get current free space on system drive
initialFreeSpace = CDbl(sysDrv.FreeSpace)

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."

Set objRegistry=GetObject("winmgmts:\\" & _
    strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys

For Each objSubkey In arrSubkeys

strValueName = "ProfileImagePath"
strSubPath = strKeyPath & "\" & objSubkey
objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strSubPath,strValueName,sProfile

wscript.echo "Cleaning profile folder: " & sProfile

DeleteFolderContents sProfile & "\Local Settings\Temp"

DeleteFolderContents sProfile & "\Local Settings\Temporary Internet Files\Content.IE5"

DeleteFolderContents sProfile & "\Local Settings\Temporary Internet Files"

DeleteFolderContents sProfile & "\AppData\Local\Google\Chrome\User Data\Default\Cache"

DeleteFolderContents sProfile & "\AppData\Local\TEMP"

DeleteFolderContents sProfile & "Application Data\Local\Microsoft\Windows\WER"

Next

' Now empty the windows\temp folder if not the same
wscript.echo "Processing folder: " & sWindows & "\Temp"
DeleteFolderContents sWindows & "\Temp"

' And ditto the system-wide Windows Error Reporting queue, if it exists.
If sWER <> "" Then
wscript.echo "Processing folder: " & sWER
DeleteFolderContents sWER
End If

wscript.Echo ""
WScript.echo "Freed " & CLng( CDbl(CDbl(sysDrv.FreeSpace) - CDbl(initialFreeSpace))/CDbl(1024*1024)) & " megabytes."

Set sysDrv = Nothing
Set objWsH = Nothing
Set objFSO = Nothing
Set objRegistry = Nothing

' ---------------------- Subroutines -------------------

sub DeleteFolderContents(strFolder)
    ' Deletes all files and folders within the given folder
    dim objFolder, objFile, objSubFolder, itemCount
    on error resume next

    itemCount = 0
    set objFolder=objFSO.GetFolder(strFolder)

    if Err.Number<>0 then
        Err.Clear
        Exit sub ' Couldn't get a handle to the folder, so can't do anything
    end if
    for each objSubFolder in objFolder.SubFolders
        objSubFolder.Delete true
        if Err.Number<>0 then
            'Try recursive delete (ensures better result)
            Err.Clear
            DeleteFolderContents(strFolder & "\" & objSubFolder.Name)
else
   wscript.stdout.write "\"
   itemCount = itemCount + 1
        end if
    next
    for each objFile in ObjFolder.Files
        objFile.Delete true
        if Err.Number<>0 then
Err.Clear ' In case we couldn't delete a file
else
wscript.stdout.write ","
       itemCount = itemCount + 1
end if
    next

    Set objFolder = Nothing

    if itemCount > 0 then
wscript.stdout.write vbCRLF
    end if
end sub

sub force_cscript
    dim args : args=""
    dim i, wshshell
    If right(lCase(wscript.fullname),11)= "wscript.exe" then
        for i=0 to wscript.arguments.count-1
            args = args & wscript.arguments(i) & " "
        next

        Set WshShell = CreateObject("wscript.shell")

        wshshell.run wshshell.ExpandEnvironmentStrings("%comspec%") & _
            " /c cscript.exe //nologo """ & wscript.scriptfullname & """" & args

        set wshshell=nothing

        wscript.quit
    end if
end sub

1 comment:

  1. Made some additions to the script which can be used on windows 7. Untested on Vista, Win 10, and up.
    (See link below)
    http://pastebin.com/SnTbeDxe

    ReplyDelete