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”


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.

You may also like:

What to Do if Outlook Cannot Connect to Gmail Acco... In case when your Outlook client is not connecting to Gmail and your credentials are not accepted, you may want to fix that. In this article, we will ...
Add Calendar Permissions in Office 365 via PowerSh... This is a tutorial on how to add calendar permissions in Office 365 for your users via PowerShell. You can add the permissions on a specific user’s ma...
How to Setup Gmail in Outlook 2016? In this article, we are going to show you how to set up Microsoft Outlook 2016 with Gmail. After MS Office Outlook installation you will see a welcome...
How to Get List of Installed Programs in Windows 1... In this simple guide, we will show you two different ways of how to get a list of installed programs in Windows 10, 8 or Windows 7 using built-in comm...
Fix Trust relationship failed issue without domain... In this article, we will discuss the causes of Trust relationship failed error and some solutions on how to restore secure channel between the worksta...
  1. Posted by Stefan

Add Your Comment