Clean Temp Function (Updated)

This is a small but effective function.
It will delete most of the stuff in your temp dir.
 

Use with close option to close all open programs
before you clean the temp dir. It will not delete
open/locked files.
drive-slim-dos-icon 

The function can also be downloaded from the Download section.
http://poweradmin.se/blog/download

 
# Clean Temp Function
# By Fredrik Wall, fredrik@poweradmin.se
# http://www.poweradmin.se/blog
#
# Last modified: 2009-12-14
# Tested on: Windows 7, Windows Server 2008 R2
# Revision history:
# 2009-12-14    ADD: -ErrorAction SilentlyContinue
#                ADD: Close option
# Example of calling the function:
# Clean-Temp
# Clean-Temp 'Close'
#
function Clean-Temp {
    param($status)
    if ($status -eq 'close') {
        # Some programs that often uses temp dir for temp files
        process iexplore* | kill
        process outlook* | kill
        process word* | kill
        process excel* | kill
        process msn* | kill

        $myTemp = "$Env:temp"
        Get-Childitem $myTemp | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
        $myTemp = $null
    }
    else  {
        $myTemp = "$Env:temp"
        Get-Childitem $myTemp | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
        $myTemp = $null
    }
}

 

Technorati Tags: ,,

No related posts.

This entry was posted in PowerShell Blogs and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>