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.

READ ALSO  How to Upgrade to a New Phone and Take Everything with You

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:

TheITBros Microsoft Office 365 Migration Guide We just migrated a company to Microsoft Office 365 and we thought we would share our experience and troubleshooting tips. We hope this saves you some ...
What To Do If Outlook cannot connect to Gmail acco... If you try to connect to Gmail in Microsoft Outlook and your credentials are not accepted, you may want to fix that. In this article we will show you ...
Add An Out Of Office Message In Outlook for A Diff... Did an employee just leave for vacation and forget to do something? Below the instructions will show you how to add an out of office message in Outloo...
How to configure Gmail with Microsoft Outlook 2016 Hello guys! In this article we are going to show you how to configure and synchronize Microsoft Outlook 2016 with Gmail. After installation you will s...
Grant Full Access to All Mailboxes in Exchange 200... This is a short tutorial on how to add full access to all mailboxes in Exchange 2007 and 2010 for a an additional user. This can come in handy when sa...