Remove-Folder for Current Logged in User?

Hello, I’m trying to get this PowerShell script below, into PSAD…

Get-Process -ProcessName Teams | Stop-Process -Force
Start-Sleep -Seconds 3
Get-ChildItem -Path $env:APPDATA\"Microsoft\teams\application cache\cache" | Remove-Item -Confirm:$false
Get-ChildItem -Path $env:APPDATA\"Microsoft\teams\blob_storage" | Remove-Item -Confirm:$false
Get-ChildItem -Path $env:APPDATA\"Microsoft\teams\databases" | Remove-Item -Confirm:$false
Get-ChildItem -Path $env:APPDATA\"Microsoft\teams\cache" | Remove-Item -Confirm:$false
Get-ChildItem -Path $env:APPDATA\"Microsoft\teams\gpucache" | Remove-Item -Confirm:$false
Get-ChildItem -Path $env:APPDATA\"Microsoft\teams\Indexeddb" | Remove-Item -Confirm:$false
Get-ChildItem -Path $env:APPDATA\"Microsoft\teams\Local Storage" | Remove-Item -Confirm:$false
Get-ChildItem -Path $env:APPDATA\"Microsoft\teams\tmp" | Remove-Item -Confirm:$false

I’ve tried using:

 Remove-Folder -Path "$env:APPDATA\Microsoft\teams\application cache\cache" 

But since it’s running as system I get:

[Installation] :: Folder [C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Microsoft\teams\application cache\cache] does not exists…

Basically what I’m trying to accomplish is:

Close Teams (I have that worked by using: Show-InstallationWelcome -CloseApps ‘Teams’ -Silent -CheckDiskSpace)
Delete the files/folders under those locations (which are user locations)…

I just can’t seem to get it working… I was originally going to have PSAD call a separate powershell script but wanted the logging…

Any ideass?

Thank you.

How are you deploying this? SCCM? If so, Since it’s Teams (which unfortunately resides in the user’s profile) you could call it as a per user deployment instead of a per-system.

If you want to run as it system, you’ll need to enumerate all profiles. This post has a good example that you should be able to leverage.
https://discourse.psappdeploytoolkit.com/t/copy-file-to-all-users-currently-logged-in-and-for-all-future-users/585/2

You probably just want to clear Teams cache?

Try this:

$loggedonUser = Get-LoggedOnUser | select -ExpandProperty UserName
$TeamsPath = [System.IO.Path]::Combine('C:\Users\', $loggedonUser,'Appdata', 'Roaming', 'Microsoft', 'Teams')

Remove-File -Path "$TeamsPath\Cache\*"

Remove-File is part of the toolkit. Same for Remove-Folder.
You’ll have to construct the other paths yourself.

I’ll give this a try, thank you! I tried running under user but UAC prompts when it launches the toolkit.

You cannot run Powershell scripts as a User in SCCM. It will always show a UAC Prompt. Except if you would disable it via GPO, but don’t do that!

Alternatively you could wrap that in a .BAT file and deploy the .BAT as a user. That would work.
But that’s really not necessary as PSADT is powerfull enough to handle user-centric tasks as well.

Worked great. Thanks so much.

1 Like

Hi Walter

know i am not at Office but I created a code that could help you for every situation in if you want to do something as currentuser.

Tomorow I will Post it.

So here is the script:

$ProfilePaths = Get-UserProfiles | Select-Object -ExpandProperty 'ProfilePath' 
        foreach ($Profile in $ProfilePaths ){
                Remove-Folder -Path "$Profile\appdata\Local\Microsoft\Teams"
                Remove-RegistryKey -Key 'HKCU\Software\Microsoft\Office\Teams'
              }

This allows you to delete the content for every account on the client, the special is that you can let sccm deploy it under system user.

I also generated a script for installation under currentuser:

$CurrentUser = Get-LoggedOnUser

then you can use for example

Execute-ProcessAsUser -UserName $CurrentUser.NTAccount -Path "%Software.exe%"