Windows Profile Cleanup Script

This script can help you to clean your users profiles without installing any third party application, you can put it as shutdown or startup script:

force_cscript

dim objWSH, sProfile, objFolder
dim objFSO, sProfileRoot, objProfileFolder
dim sTemp, sWindows

set objFSO=CreateObject("Scripting.FileSystemObject")

' Get user profile root folder
set objWSH = CreateObject("WScript.Shell")
sTemp = objWSH.ExpandEnvironmentStrings("%TEMP%")
sWindows = objWSH.ExpandEnvironmentStrings("%WINDIR%")
sProfile = objWSH.ExpandEnvironmentStrings("%USERPROFILE%")
sProfileRoot=objFSO.GetFolder(sProfile).ParentFolder.Path
set objWSH=nothing
set objProfileFolder=objFSO.GetFolder(sProfileRoot)
Set SystemSet = GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem") 
for each System in SystemSet 
  sysversion = System.Version
next 
OSVersionNum = Left(sysversion, 1)

If OSVersionNum = 5 Then ' If OS is Windows XP
    for each objFolder in objProfileFolder.SubFolders
        select case LCase(objFolder.Name)
            case "all users": ' do nothing
            case "default user": ' do nothing
            case "localservice": ' do nothing
            case "networkservice": ' do nothing
            case else:
                wscript.echo "Processing profile: " & objFolder.Name
                sProfile=sProfileRoot & "\" & objFolder.Name
                DeleteFolderContents sProfile & "\Local Settings\Temp"
                DeleteFolderContents sProfile & "\Local Settings\Temporary Internet Files\Content.IE5"
                DeleteFolderContents sProfile & "\Local Settings\Temporary Internet Files"
                DeleteFolderContents sProfile & "\Local Settings\Application Data\Microsoft\Office\14.0\OfficeFileCache"
                DeleteFolderContents "C:\Windows\Temp"
                DeleteAFile("C:\Windows\AgentInstall64.msi")
                DeleteAFile("C:\Windows\AgentInstall.msi")
                DeleteAFile("C:\Windows\VMAgent-64-5.msi")
                DeleteAFile("C:\VMware View Agent64.msi")
        end select
    next
End if
If OSVersionNum = 6 Then 'If OS is Windows Vista and above
    wscript.echo "Processing profile: " & objProfileFolder
    for each objFolder in objProfileFolder.SubFolders
    wscript.echo "Processing profile: " & objFolder.Name
    next
    for each objFolder in objProfileFolder.SubFolders
        select case (objFolder.Name)
            case "Public": ' do nothing
            case "Default": ' do nothing
            case "Default User": ' do nothing
            case "All Users": ' do nothing
            case else:
                wscript.echo "Processing profile: " & objFolder.Name
                sProfile=sProfileRoot & "\" & objFolder.Name
                DeleteFolderContents sProfile & "\AppData\Local\Temp"
                DeleteFolderContents sProfile & "\AppData\Local\Microsoft\Windows\Temporary Internet Files"
                DeleteFolderContents "C:\Windows\Temp"
                DeleteAFile("C:\Windows\AgentInstall64.msi")
                DeleteAFile("C:\Windows\AgentInstall.msi")
                DeleteAFile("C:\Windows\VMAgent-64-5.msi")
                DeleteAFile("C:\VMware View Agent64.msi")
        end select
    next
End if
' Now delete the folder given by the TEMP environment variable
wscript.echo "Processing folder: " & sTemp
DeleteFolderContents sTemp
' And the windows\temp folder
wscript.echo "Processing folder: " & sWindows & "\Temp"
DeleteFolderContents sWindows & "\Temp"

Sub DeleteAFile(filespec)
   on error resume next
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   fso.DeleteFile(filespec)
End Sub

sub DeleteFolderContents(strFolder)
    ' Deletes all files and folders within the given folder
    dim objFolder, objFile, objSubFolder
    on error resume next
    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)
        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
    next
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

If you want to run it as scheduled task, you need to run that via batch file with a local administrator account.

This batch file create a local administrator account, run the previous script under the user’s privilege and then remove the user.

ver | findstr /i "6\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto Win7
FOR /D %%X IN ("C:\Documents and Settings\temptask*") DO RD /S /Q "%%X"
net user /add TempTask 1234!@#$qwer
net localgroup administrators TempTask /add
Copy \\SharedFolder\Auto_Clean.vbs C:\Windows /Y
schtasks /create /S %computername% /RU %computername%\TempTask /RP 1234!@#$qwer /TN "TempTask" /TR C:\Windows\Auto_Clean.vbs /SC Daily /ST 05:00 /f
schtasks /run /TN TempTask
timeout /t 30
schtasks /delete /TN TempTask /F
net user /del TempTask
FOR /D %%X IN ("C:\Documents and Settings\temptask*") DO RD /S /Q "%%X"
Goto :end
:Win7
FOR /D %%X IN (C:\users\temptask*) DO RD /S /Q "%%X"
net user /add TempTask 1234!@#$qwer
net localgroup administrators TempTask /add
Copy \\SharedFolder\Auto_Clean.vbs C:\Windows /Y
schtasks /create /S %computername% /RU %computername%\TempTask /RP 1234!@#$qwer /TN "TempTask" /TR C:\Windows\Auto_Clean.vbs /SC Daily /ST 05:00 /f /RL Highest
schtasks /run /TN TempTask
timeout /t 30
schtasks /delete /TN TempTask /F
net user /del TempTask
FOR /D %%X IN (C:\users\temptask*) DO RD /S /Q "%%X"
Goto :end
:end
  • The scripts tested under Windows XP and Windows 7.

Further Reading

Veeam Backup & Replication – Re-IP Rule on Linux VM

Recommended Settings on Windows 10 for VDI

Davoud Teimouri

Davoud Teimouri is as a professional blogger, vExpert 2015/2016/2017/2018/2019, VCA, MCITP. This blog is started with simple posts and now, it has large following readers.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newsletter and join other subscribers

Holler Box