powershell outlook

Clean Out Temporary Outlook Files via PowerShell


Hey guys! If you use Microsoft Outlook on the terminal server, you should know that Outlook is saving temporary files. It creates this sort of files whenever you add or view an attachment. You can find the folder with the temporary files here:

C:\Users\%username%\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Outlook\

You should know that this folder won’t be cleaned out automatically.

But what if there are a lot of users using your server? One day the folder with temporary files will get full, so you will need to clean it out.

Here is a simple solution, so let’s start.

We are going to use PowerShell. What we need to do is just run the script, which is going to literate all users in the parent directory and recursively cleans the files of each user.

It is reasonable to run this script in the night, right before the backup of your terminal server.

function Get-Tree($Path,$Include='*') {
 @(Get-ChildItem $Path -Recurse -Include $Include) | sort pspath -Descending -unique
 }
function Remove-Tree($Path,$Include='*') {
 Get-Tree $Path $Include | Remove-Item -force -recurse
 }
foreach ($LS_Users in ls “C:Users” | ?{$_.psiscontainer -eq “true”}) {
 $Temp_Outlook_Content = Join-Path $LS_Users.fullname "AppDataLocalMicrosoftWindowsTemporary Internet FilesContent.Outlook"
 # Get-Tree $Temp_Outlook_Content | select fullname
 Remove-Tree $Temp_Outlook_Content
 }

As we can see, there is a short, but very versatile script. It will clean out all the temporary files recursively. Also, you can use this script for other folders.

How it works

For each parent directory “C:Users”

foreach ($LS_Users in ls “C:Users” | ?{$_.psiscontainer -eq “true”})

Select all the files and directories (for example).

“C:UsersRDS.UserAppDataLocalMicrosoftWindowsTemporary Internet FilesContent.Outlook”

Code:

Get-Tree $Temp_Outlook_Content | select fullname

This string of code prints the information about files and folder that are going to be deleted.

And the finally, we need to clean out all the files and folders by using this string.

Remove-Tree $Temp_Outlook_Content

That’s all. It’s a simple way to clean out temporary files by using PowerShell.

Comments
  1. Posted by Stefan

Add Your Comment