Temporary Internet Files

Introduction

If you manage Windows terminal servers, or Citrix XenApp servers you will know all about temporary internet files, and the amount of space they take up on your servers and user profile folders.

There are ways to reduce the impact on profile folders by using GPOs to not copy the folder about, but this still could leave them on the server.

 

Removal Script

I have customised a script I found somewhere on the internet to remove the temporary internet folders for all users on a particular server.  The script enumerates C:\USERS for every user and if a Temporary Internet Files folder exists, it is deleted and a new one created in its place.  This is a quicker method than removing specific files.  It will also delete and recreate the users Temp folder.

 

The Script

@ECHO OFF
@ECHO Started: %time%
@PUSHD "C:\Users"

FOR /D %%F IN (*.*) DO (
    IF EXIST "%%F\AppData\Local" (
        @ECHO Processing user: %%F
        @PUSHD "%%F\AppData\Local"

        IF EXIST "Microsoft\Windows\Temporary Internet Files" (
            @ECHO Removing directory 'Temporary Internet Files'
            @PUSHD "Microsoft\Windows"
            IF [%%F] == [%username%] (
                @PUSHD "Temporary Internet Files"
                FOR /F %%E IN ('DIR /AD /B') DO (
                    @PUSHD %%E
                    FOR /F %%G IN ('DIR /AD /B') DO (
                        @ECHO Removing %%G
                        RD /S /Q %%G
                    )
                    POPD
                )
                POPD
            ) ELSE (
                RD /S /Q "Temporary Internet Files"
                @ECHO Creating new 'Temporary Internet Files' directory
                MD "Temporary Internet Files"
            )
            POPD
        )

        IF EXIST "Temp" (
            @ECHO Removing directory 'TEMP'
            IF [%%F] == [%username%] (
                @PUSHD "Temp"
                FOR /F %%E IN ('DIR /AD /B') DO (
                    @PUSHD %%E
                        FOR /F %%G IN ('DIR /AD /B') DO (
                            @ECHO Removing %%G
                            RD /S /Q %%G
                        )
                        POPD
                    )
                    POPD
                ) ELSE (
                    RD /S /Q "Temp"
                    @ECHO Creating new 'TEMP' directory
                    MD "Temp"
            )
        )
        POPD
        @ECHO. 
    )
)
POPD
@ECHO Finished: %time%
PAUSE

 

Instructions For Use

  1. Select the code above and save as plain text file with an extension of either .CMD or .BAT
  2. Copy to one of your terminal servers
  3. Right click and choose Run As Administrator

If your users folder is not in C:\Users, make sure you change line 3 above to the correct location.

Leave a Reply

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