Quantcast
Channel: GARYTOWN ConfigMgr Blog
Viewing all articles
Browse latest Browse all 207

Temp File Cleanup–Windows, IE, Chrome, Java on All Profiles.

$
0
0

Recently I needed to clean up space on computers with low free space, and wrote this primative batch file that would log the deleted files, along with free space before & after.

The Scripts will perform and log these things to a server file share:

  1. Set Registry Keys to run Windows Cleanup Manager & Run Cleanup – based on deploymentbunny
  2. Call vbs file to delete IE, Chrome, Java Cache & Windows Temp files – based on this script
  3. Set Page file to 2GB

To do this, you need to have a share that is pretty wide open, write access from “domain computers”, or what I’ve done is map a drive in the script with a user account that only has access to that share.

 

Scripts located here (Shown below, but recommend downloading, as copy / paste isn’t always the best)

image

WindowsCleanUp.cmd (Main Script, the one that calls the others) =
———————

REM 1- Maps Drive to Network Share for Log File – Use fine and replace to replace Server\Share with your info
REM 2- Sets Registry Keys and Runs CleanMgr.exe using those settings (Borrowed from deploymentbunny )
REM 3- Delete _SMSTaskSequence folder if exist – I’ve seen this in our enviroment after pushing some software using at TS.
REM 4- Delete Internet – Calls CMD File that will delete: Java Cache, IE Temp Files, Chrome Temp Files, Windows Temp Files for all Users
REM 4  – This will build a batch file “C:\TempFileRemoval.bat” containing the directories in which to delete.
REM 5- Sets Pagefile to 2GB
REM 6- Finishes Logging, and shows current FreeSpace

@setlocal enableextensions enabledelayedexpansion
@echo off

for /f “usebackq delims== tokens=2” %%x in (`wmic logicaldisk where “DeviceID=’C:'” get       FreeSpace /format:value`) do set FreeSpace=%%x
echo !FreeSpace:~0,-10!,!FreeSpace:~2,-8!GB
set OrgFreeSpace=!FreeSpace:~0,-10!,!FreeSpace:~2,-8!GB

REM MAP Drive to allow Log File Dumps
net use /persistent:no
net use y: \\server\share /user:domain\user password
echo _  >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
echo _  >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
echo —————–NEW RUN———————————————————— >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
echo Current Free Space: !FreeSpace:~0,-10!,!FreeSpace:~2,-8!GB—————————– >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
echo Current Time Stamp:             %date% –  %time% >>\\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
echo _  >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log

computername%-CleanUp.log
echo ——–STARTING WINDOWS CLEANMGR ————– >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
REM This Adds Registry Key for SageSet then Runs those options

reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Memory Dump Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Service Pack Cleanup” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error minidump files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Upgrade Discarded Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\User file versions” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Archive Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Queue Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Archive Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Queue Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows ESD installation files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f
reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Upgrade Log Files” /v StateFlags5432 /d “00000002” /t “REG_DWORD” /f

REM Runs Cleanup Manager using the Settings created via the Registry Key

cleanmgr.exe /SAGERUN:5432 >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
echo off
ping 10.1.1.1 >nul
echo on
echo ——–FINISHED WINDOWS CLEANMGR ————– >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
echo Current Time Stamp:             %date% –  %time% >>\\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log

REM Delete Folder:
if exist “C:\_SMSTaskSequence” rmdir “C:\_SMSTaskSequence” /S /Q

echo off
ping 10.1.1.1 >nul
echo on

echo ——–Delete Temp Files ——-WindowsCleanUp\DeleteInternetTempFiles.cmd——- >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
REM Delete Temp Files
cmd.exe /c DeleteInternetTempFiles.cmd >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
echo Current Time Stamp:             %date% –  %time% >>\\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
echo off
ping 10.1.1.1 >nul
echo on
echo ———–SET PAGE FILE SIZE 2GB—————- >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
REM Change Pagefile to 2GB
REM disable the automatic pagefile
wmic computersystem where name=”%computername%” set AutomaticManagedPagefile=False
REM create a pagefile to replace the C:\pagefile.sys with a fixed size of 2048 Megabyte
wmic pagefileset where name=”C:\\pagefile.sys” set InitialSize=2048,MaximumSize=2048

echo Current Time Stamp:             %date% –  %time% >>\\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log

for /f “usebackq delims== tokens=2” %%x in (`wmic logicaldisk where “DeviceID=’C:'” get       FreeSpace /format:value`) do set FreeSpace=%%x
echo Orginal Free Space: %OrgFreeSpace% —————————– >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log
echo Free Space Now:       !FreeSpace:~0,-10!,!FreeSpace:~2,-8!GB—————————– >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log

echo ————PROCESS COMPLETE————- >> \\server\share\_PreventativeMaintenanceLogs\%computername%-CleanUp.log

REM Delete Drive Mapping
net use y: /delete

————————

DeleteInterentTempFiles.cmd=
————————

REM – https://community.spiceworks.com/scripts/show/1082-delete-temporary-internet-files-temp-files-from-local-profiles-windows-2008-win7
cscript.exe DeleteInternetTempFiles.vbs

c:\TempFileRemoval.bat

————————

DeleteInternetTempFiles.vbs=
————————

on error resume next

Dim oFs
Dim oFolder
Dim oFile
Dim Folder
Dim FolderSpec
Dim FileCollection
Dim objShell

DriveLetter = “C”

FolderSpec =  DriveLetter & “:\Users”
Set oFs = CreateObject(“Scripting.FileSystemObject”)

Set oFolder = oFs.GetFolder(FolderSpec)

Set FileCollection =  oFolder.SubFolders

On Error Resume Next

For Each subfolders In FileCollection
                ‘wcript.echo SubFolders & “\TempInternetFiles\*.*”
        ‘usrTempCD = “CD “””   & SubFolders & “\AppData\Local\Microsoft\Windows\Temporary Internet Files”””
                usrTempFolder = “del “””   & SubFolders & “\AppData\Local\Microsoft\Windows\Temporary Internet Files””” & ” /F /S /Q”
                usrTempFolder2 = “del “””   & SubFolders & “\AppData\Local\Temp\*.*””” & ” /F /S /Q”
                usrTempFolder3 = “del “””   & SubFolders & “\AppData\Local\Google\Chrome\User Data\Default\Cache\*.*””” & ” /F /S /Q”
                usrTempFolder4 = “del “””   & SubFolders & “\AppData\LocalLow\Sun\Java\Deployment\cache\*.*””” & ” /F /S /Q”
        ‘wscript.echo usrTempFolder

Set filesys = CreateObject(“Scripting.FileSystemObject”)
Set filetxt = filesys.CreateTextFile(“C:\TempFileRemoval.bat”, True)
path = filesys.GetAbsolutePathName(“C:\TempFileRemoval.bat”)
getname = filesys.GetFileName(path)
‘filetxt.Writeline “C:”
filetxt.Writeline usrTempCD
filetxt.Writeline usrTempFolder
filetxt.Writeline usrTempFolder2
filetxt.Writeline usrTempFolder3
filetxt.Writeline usrTempFolder4
               
Next
‘filetxt.Writeline “Del c:\TempFileRemoval.bat”

filetxt.Writeline “del c:\windows\temp\* /F /S /Q”

———————–

The scripts create a log file on a file server:
image
They can get quite large due to it tracking every file that gets deleted.

 

Sample from the Log File: (Use CMTrace to watch Log populate live)
My Test Machine is pretty clean, I tried to create some Temp files for the Logs.
image
image

image

Here is part of the log of one that actually had something CleanMgr could clean up:
image

The next time the computer reboots, it will finish the Windows Update Removal Cleanup, can take several minutes.

 

I have this as a package / program, that I run against a Query Based collection.  If machines get low disk space, they will automatically run this program.  I also run this on computers that McAfee tags as having had been infected, just to clear out temp files, where the infection typically was.


Viewing all articles
Browse latest Browse all 207

Latest Images

Trending Articles



Latest Images