Of course you have to run this code in an elevated console.
Regardless of that …
When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
I wanted to ask Is it possible in C# to run this powershell script?
I am trying to build a C# app that allows users to clear cache without using powershell or complicated steps.
I am not a C# developer … shouldn’t it be easier to do it natively in C# than starting a PowerShell session from within a C# program?
The snippet I shared above will delete the desired files and folders in ALL user profiles. Usually users do not have access to the profiles of other users. If you only want to clean up the profile of the currently logged on user you may use your initial approach.
Regardless of that … I never had to clean up my Teams cache yet. If you have some problems with that you may solve the root cause for those problems instead of tinkering a wacky workaround.