Can powershell delete a folder under a roaming profile for any user?

Here is the code I used and it works on just me:
Remove-Item -Path C:\Users\bruce.w.barfield3\AppData\Roaming\Microsoft\Teams -Recurse

I want this to work on any PC the script runs on. So I created this:
Remove-Item -Path C:\Users$env:username\AppData\Roaming\Microsoft\Teams -Recurse

This code however gives access denied errors. How can I make this script to it works on any user?

Bruce,
Welcome to the forum. :wave:t3:

First of all … you’re overcomplicating this … something like this is enough:

Remove-Item -Path C:\Users\*\AppData\Roaming\Microsoft\Teams -Recurse -Force

Of course you have to run this code in an elevated console. :wink:

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.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Oh wow! thank you so much! Im new to powershell XD
I know enough to be dangerous lol

:smirk:

In this case I’d recommend reading about the risk mitigation parameter -WhatIf most potentially harmful cmdlets offer …

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. :smirk: :man_shrugging:t3:

You may also want to make sure Teams is not running, so the files are not in use when attempting to delete the files.