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 is not connecting to Gmail and your credentials are not accepted, you may want to fix that. In this article we will show you...
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 permissions onto a specific mailbox, or...
How to Setup Gmail in Outlook 2016 Hello guys! In this article we are going to show you how to setup Microsoft Outlook 2016 with Gmail. After installation you will see a welcome screen,...
How to copy files with BITS using PowerShell? If you use local (and global) networks, you might know that files between systems are transferring by using SMB, FTP or HTTP protocols. The problem wi...
Removing Old and Unused Drivers from Driver Store ... Each time you install or update your device driver, Windows OS (since Vista) continues to store the old version of the driver in the system Driver Sto...

Add Your Comment