I had the same question, but motivated by the desire to aid my efforts to finish off malware cleanup operations. Here's the command script I wrote with an eye toward modularization so it could be extended easily for future OSes and temp file locations (I wrote this before learning PowerShell and haven't bothered to update it). Because it accesses every users profile folder on the machine as well as Windows system folders, the script must be run with elevated privileges.
@echo off
Rem Temp File Purging Tool v1.2.0
Rem Written by Twisty. Created 1/19/2011. Modified 6/28/2011.
Rem
Rem This script deletes temp files in locations where malware likes to write its initial
Rem files for infection and also where standard users have write permissions.
Rem
Rem This tool isn't likely to be as helpful to clean systems on which users run with
Rem Admin permissions. If you let your users run with Admin permissions you by extension
Rem give much of the malware on the Internet permission to do as it pleases on your workstations.
Rem Identify version of Windows
SET WinVer=Unknown
VER | FINDSTR /IL "5.1." > NUL
IF %ERRORLEVEL% EQU 0 SET WinVer=XP
rem 5.2 is actually Server 2003, but for our purposes it's the same as XP
VER | FINDSTR /IL "5.2." > NUL
IF %ERRORLEVEL% EQU 0 SET WinVer=XP
VER | FINDSTR /IL "6.0." > NUL
IF %ERRORLEVEL% EQU 0 SET WinVer=VISTA
rem 6.1 is actually Windows 7, but for our purposes it's the same as Vista
VER | FINDSTR /IL "6.1." > NUL
IF %ERRORLEVEL% EQU 0 SET WinVer=VISTA
rem Ask user the version if we cannot automatically determine
If Not "%WinVer%" EQU "Unknown" Goto :SetUserProfPath
Set /P Response="Select OS [X]P, [V]ista/7: "
If /i "%Response%" EQU "X" Set WinVer=XP
If /i "%Response%" EQU "V" Set WinVer=VISTA
If "%WinVer%" EQU "" Echo Invalid response. Exiting.&goto :eof
:SetUserProfPath
If %WinVer% EQU XP (
Set UserProfileRootPath=C:\Documents and Settings
) Else (
Set UserProfileRootPath=C:\Users
)
Call :RemoveSubfoldersAndFiles %SystemRoot%\Temp
Rem Walk through each user profile folder
Rem This convoluted command is necessary to ensure we process hidden and system folders too
for /f "delims=" %%D in ('dir /ad /b "%UserProfileRootPath%"') DO Call :ProcessProfileFolder %UserProfileRootPath%\%%D
Echo.
Echo Finished! Press a key to exit...
Pause>Nul
goto :EOF
:ProcessProfileFolder
Set FolderName=%*
Rem Leave if it's not a user profile folder
If Not Exist "%FolderName%\ntuser.dat" goto :EOF
Rem Leave it's a profile folder on the exclude list
If /I "%FolderName%" EQU "%UserProfileRootPath%\Default" goto :EOF
If /I "%FolderName%" EQU "%UserProfileRootPath%\Default User" goto :EOF
If /I "%FolderName%" EQU "%UserProfileRootPath%\NetworkService" goto :EOF
If /I "%FolderName%" EQU "%UserProfileRootPath%\LocalService" goto :EOF
Set UserProfilePath=%FolderName%
Rem Clean up these folders
If %WinVer% EQU XP (
Call :RemoveSubfoldersAndFiles %UserProfilePath%\Local Settings\Temp
Call :RemoveSubfoldersAndFiles %UserProfilePath%\Local Settings\Temporary Internet Files
Call :RemoveSubfoldersAndFiles %UserProfilePath%\Application Data\Sun\Java\Deployment\cache
) Else (
Call :RemoveSubfoldersAndFiles %UserProfilePath%\AppData\Local\Temp
Call :RemoveSubfoldersAndFiles %UserProfilePath%\AppData\LocalLow\Temp
Call :RemoveSubfoldersAndFiles %UserProfilePath%\AppData\LocalLow\Sun\Java\Deployment\cache
Call :RemoveSubfoldersAndFiles %UserProfilePath%\AppData\Local\Microsoft\Windows\Temporary Internet Files
)
goto :EOF
:RemoveSubfoldersAndFiles
Set FolderRootPath=%*
Rem Confirm target folder exists
If Not Exist "%FolderRootPath%" Goto :EOF
Rem Make the folder to clean current and confirm it exists...
CD /D %FolderRootPath%
Rem Confirm we switched directories
If /I "%CD%" NEQ "%FolderRootPath%" Goto :EOF
Rem ...so that this command cannot delete the folder, only everything in it
Echo Purging %CD%
RD /S /Q . >>nul 2>>&1
goto :EOF
Extending the Script's Functionality
Part of the script's extensibility is found in its use of the :RemoveSubfoldersAndFiles
procedure. To delete the contents of a folder, simply call this procedure and pass a folder path as the only parameter (without double-quotes). The routine will gracefully handle paths that don't exist, folders that cannot be accessed for any reason, or the case where some files or folders below the path are in use or otherwise refuse to be deleted.
To clean up additional folders found in each user's profile
In the section Rem Clean up these folders
add additional calls to the :RemoveSubfoldersAndFiles subroutine. For example, to delete everything in each user's \AppData\Local\Microsoft\Windows\Temporary Internet Files
folder add the line:
Call :RemoveSubfoldersAndFiles %UserProfilePath%\AppData\Local\Microsoft\Windows\Temporary Internet Files
Note the use of the script-defined %UserProfilePath%
variable instead of the typical %USERPROFILE%
variable. The script's version is dynamically updated as the script iterates through each user profile on the machine.
To clean up folders found outside user profiles
In the :SetUserProfPath
subroutine, again add calls to the :RemoveSubfoldersAndFiles procedure. For example:
Call :RemoveSubfoldersAndFiles C:\Temp