Clean temp files from User Profiles folder with PowerShell

As IT Engineer I am responsible for maintaining servers that have a limited disk space available. One of my tasks is to remove temporary files from user profiles on terminal servers.

Since I am in favor of automating anything that I need to do multiple times, I decided to create a PowerShell script to clear the Temp folder under each profile on the terminal servers, as well as search for specific extensions that other third party applications create for temporary files.

Script functions:

  • Find all the directories under the users profile folder
  • Gets all the files under each profile that matches the extensions supplied and deletes the files
  • Gets all the files and folders under the profile’s Temp directory and deletes the files and folders
  • The Force option is not used for deleting files. Since certain files may still be in use or may be marked as system files and shouldn’t be deleted.

This script has the following requirements:

  • The folder where the user profiles are located
  • [Optional] The extensions that needs to be removed
  • Read-Write access to all profile folders
  • The script must run as Administrator

The syntax:

Clean-Profiles.ps1 -ProfilesPath -Extensions

For example:

D:\PowerShellScripts\Clean-Profiles.ps1 -ProfilesPath “C:\Users” -Extensions “*.tmp”,”*.log”

Download

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s