Clear Microsoft Teams cache using PowerShell – Intune compatible

In this blog article I describe the functionality (documentation) of my latest script for Microsoft Teams.

It is about deleting the Teams cache for one or more users.
This can be done in different ways.
On the one hand, there is a really great tool by Johannes Huber under the following link:

Teams Cache löschen per Tool (Teams Cache Cleaner)

The problem here, however, is that this tool must be run on each affected device.

In principle, this is much easier. We can execute a small BAT file which also deletes the team cache.

However, it becomes more difficult with the following points:

How can I distribute this BAT file to the user computers that are affected? How can I ensure that the BAT file is only executed once? How can the script be executed so that the user does not have to interact? Finally, how can we ensure logging?

 

I have addressed these questions and written a PowerShell script that solves all these problems.

The script is written in such a way that we can distribute it via Microsoft Intune.

 

Functionality:

If the script is executed for the first time, it will be checked whether the directory C:\MDM\Logging exists. If this is not the case, the directory is created accordingly.

This directory serves as data storage for the created log file.

The next step is to start PowerShell Transcripting.
Now another, temporary directory is created.

Then the required BAT file is downloaded from my GitHub repository and executed.

The BAT file itself looks like this
When the process is complete, the BAT file and the second directory are deleted.

 

It should also be noted that the Teams client restarts automatically.

 

In the directory C:\MDM\Logging we find the created log file.

 

 

What happens during the second execution of the script?

Here, too, it is checked whether the directory C:\MDM\Logging already exists. Since the script has already run through once successfully, this folder usually already exists.
After that, we can also look at the generated log file here. (If required)
However, if the script is actually executed again, the registry key shown here must be deleted.

HKCU:Software\Microsoft\MSB365_Teams_clear_cache_Tool

 

The script itself references this entry. If it does not exist, the script is executed accordingly, and then this entry is created.

As already mentioned, this prevents the cache from being deleted with every restart.

 

Script packaging for Microsoft Intune

In the Endpoint Manager Portal we go to Devices > Scripts
Click on Add
Give the process a name
Select the script and use the configurations as shown in the picture.
Assign the script to the appropriate group
Complete the deployment.

 

 

You can find the link to the script further down in this article. Here you can see transparently how it works and how the BAT file is configured.

 

As always, the following applies to my contributions: These are freely available to you and can be used free of charge. It is not allowed to resell my scripts! However, it is allowed to share them.

I recommend to test the script in a test environment before you use it in production. I do not take any responsibility for possible damage to the infrastructure.

 

You can find the script on my GitHub page


Leave a Reply

Your email address will not be published. Required fields are marked *