Oct 2, 2010
Delete Temporary Internet Files from All Profiles in Windows XP Using PowerShell
As a systems administrator, I re-image systems quite often. Part of my re-imaging process entails copying the “Documents and Settings” folder containing the user profiles to a disk on my server, dropping a fresh image on the drive, and then copying the user profiles into C:\Recovered_Profiles before shipping it back to the location that sent it in. This allows the local IT team member to move the data into the users’ new profiles after their first logon.
The transfer of user profiles can take a very long time depending on the amount of data in each profile, but also upon how many files are in the Temp and Temporary Internet Files folders inside each profile. It’s not the size of the files in those folders that will cause extended transfer times, but the sheer number of small files in the folders. There can be thousands upon thousands of files in the Temporary Internet Files folder, and each file accounts for a separate read transaction on the hard drive. For this reason, I wrote a VBScript a year or so ago to clear out these two folders, but it never really worked 100% of the time. I’d still end up with 2-3 profiles (out of 20 or so) that had temp files, but it was better than nothing.
Enter Windows PowerShell. I was able to condense the 60 or so lines of VBScript required to empty those two folders into the script below, which is fewer than 10 lines of actual code. In fact, there are nearly twice the number of lines of comments in the script than lines of code when you count the “help” section at the top of the script. I’ve tested this script on 10 different machines, and it hasn’t left any temp files behind other than files that were in use at the time I ran the script (most commonly the index.dat file in the IE cache folder).
UPDATE: I have posted a modified version of this script for use on Windows 7 and Vista machines in the post titled Delete Temporary Internet Files from All Profiles in Windows 7 and Vista Using PowerShell.
#==================================================================================
#
# Delete_Temp_Files_XP.ps1
#
# Author: Bill Clark (http://bitsoftech.net/)
#
# This script deletes all files in all users' Temp and Temporary Internet Files
# folders on a Windows XP machine.
#
#==================================================================================
<#
.SYNOPSIS
Deletes all files and subdirectories in %UserProfile%\Local Settings\Temp and
%UserProfile%\Local Settings\Temporary Internet Files for all user profiles on the machine.
.PARAMETER
No parameters are accepted by this script.
.EXAMPLE
Delete_Temp_Files_XP.ps1
.NOTES
The script should be run while logged in as Administrator, or run with Administrator privileges.
The script will not remove any files that are in use by other processes, and will throw an error upon encountering any in use files.
#>
# Create array containing all user profile folders
$colProfiles = Get-ChildItem "C:\Documents and Settings\" -Name
# Remove the "All Users" profile from the array
$colProfiles = $colProfiles -ne "All Users"
# Removes temporary files from each user profile folder
ForEach ( $objProfile in $colProfiles ) {
# Remove all files and folders in user's Temp folder
Get-ChildItem "C:\Documents and Settings\$objProfile\Local Settings\Temp\*" -recurse | remove-item -force -recurse
# Remove all files and folders in user's Temporary Internet Files. The -force switch on Get-ChildItem gets hidden directories as well.
Get-ChildItem "C:\Documents and Settings\$objProfile\Local Settings\Temporary Internet Files\*" -recurse -force | remove-item -force -recurse
}

Hello, I am intrigued by this clean up script. however, when i try it on a windows XP machine it does not work. i have tried simplifying it to see if there was an error but I am not successful. I have saved and run it as a ps1 file. is that correct?
thank you.
Hi Lisa, I’m sorry for the delayed reply. The script should be saved as a .ps1 file, that is correct. You will run the script from within the PowerShell console app. What error are you getting? You may need to modify your system’s execution policy for PowerShell scripts. I recommend using the following command:
Set-ExecutionPolicy RemoteSigned
That command should be run in the PowerShell console. It will allow your system to run locally-created scripts, but will require scripts downloaded from the Internet to be digially signed by a trusted publisher. This allows for the best mix of security and convenience. For more information on execution policies, see this page: http://technet.microsoft.com/en-us/library/dd347641.aspx