Bits of Tech

Icon

Tips & Tricks from IT Pros


Delete Temporary Internet Files from All Profiles in Windows 7 or Vista Using PowerShell

This is just a minor update to the previous script, modified for Windows 7 and Vista machines.

#==================================================================================
# Delete_Temp_Files_W7.ps1
#
# Author: Bill Clark (http://bitsoftech.net/)
#
# This script deletes all files in all users' Temp and Temporary Internet Files
# folders on a Windows Vista or Windows 7 machine.
#
#==================================================================================

<#

.SYNOPSIS

Deletes all files and subdirectories in %UserProfile%\AppData\Local\Temp and
%UserProfile%\AppData\Local\Microsoft\Windows\Temporary Internet Files for all user
profiles on the machine.

.PARAMETER

No parameters are accepted by this script.

.EXAMPLE

Delete_Temp_Files_W7.ps1

.NOTES

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:\Users\" -Name
# Remove the All Users profile from the array
$colProfiles = $colProfiles -ne "Public"

# 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:\Users\$objProfile\AppData\Local\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:\Users\$objProfile\AppData\Local\Microsoft\Windows\Temporary Internet Files\*" -recurse -force | remove-item -force -recurse
}

Technorati Tags: PowerShell, Scripting

Related Posts

Category: Windows

Tagged: PowerShell, Scripting

Bookmark and Share

2 Responses

  1. PC Repair BirminghAMNo Gravatar says:

    I created a simple BAT file to do this, for win xp vista and 7 but it only deletes the current user… can you advise on how to change to all users to delete these files?

    http://www.theeasypc.co.uk/clean.bat

    Lee

    • BillNo Gravatar says:

      Lee, it’s a lot more difficult than you’d think. I initially started off with a batch file, but to get it to remove the files for all users, I found it easier to use VBScript, because I could read the contents of the Documents and Settings folder into an array and then use a “for each” loop to go through every user. When I started learning PowerShell, I chose to port that VBScript to PowerShell, and found it even easier than VBScript.

      My suggestion would be to just stick with PowerShell, since it’s installed by default in Windows Vista and Windows 7, and it’s installed as a Windows Update on Windows XP (provided your machines are all patched up to date with all critical and recommended patches/features).

Leave a Reply