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

 

#==================================================================================
# Delete_Temp_Files_W7.ps1
#
#
# 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%AppDataLocalTemp and
%UserProfile%AppDataLocalMicrosoftWindowsTemporary 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$objProfileAppDataLocalTemp*" -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$objProfileAppDataLocalMicrosoftWindowsTemporary Internet Files*" -recurse -force | remove-item -force -recurse
Advertisements

Powered by WordPress.com.

Up ↑