{"id":9216,"date":"2025-09-03T08:49:40","date_gmt":"2025-09-03T08:49:40","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=9216"},"modified":"2025-09-03T08:49:41","modified_gmt":"2025-09-03T08:49:41","slug":"powershell-to-remove-old-teams-stick-in-cprogram-fileswindowsapps","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/powershell-to-remove-old-teams-stick-in-cprogram-fileswindowsapps","title":{"rendered":"Powershell to Remove Old Teams stick in C:\\Program Files\\WindowsApps"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code class=\"\"># Run as Administrator to access C:\\Program Files\\WindowsApps\n# Ensure script has permissions to modify protected folders\n\n# Set the path to WindowsApps\n$windowsAppsPath = \"C:\\Program Files\\WindowsApps\"\n\n# Get all MSTeams folders\n$teamsFolders = Get-ChildItem -Path $windowsAppsPath -Directory -Filter \"MSTeams*\" -ErrorAction SilentlyContinue\n\nif ($teamsFolders.Count -eq 0) {\n    Write-Output \"No MSTeams folders found in $windowsAppsPath\"\n    exit\n}\n\n# Find the folder with the latest creation time\n$latestFolder = $teamsFolders | Sort-Object CreationTime -Descending | Select-Object -First 1\nWrite-Output \"Latest MSTeams folder (will be kept): $($latestFolder.Name)\"\n\n# Take ownership and grant permissions for all MSTeams folders\nforeach ($folder in $teamsFolders) {\n    Write-Output \"Taking ownership of: $($folder.FullName)\"\n    takeown \/f $folder.FullName \/r \/d y\n    icacls $folder.FullName \/grant Administrators:F \/t\n}\n\n# Delete all MSTeams folders except the latest one\nforeach ($folder in $teamsFolders) {\n    if ($folder.Name -ne $latestFolder.Name) {\n        try {\n            Write-Output \"Deleting: $($folder.FullName)\"\n            Remove-Item -Path $folder.FullName -Recurse -Force -ErrorAction Stop\n            Write-Output \"Successfully deleted: $($folder.Name)\"\n        } catch {\n            Write-Error \"Failed to delete $($folder.Name): $_\"\n        }\n    }\n}\n\nWrite-Output \"Cleanup complete. Only $($latestFolder.Name) remains.\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9216","post","type-post","status-publish","format-standard","hentry","category-research"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/9216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=9216"}],"version-history":[{"count":1,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/9216\/revisions"}],"predecessor-version":[{"id":9217,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/9216\/revisions\/9217"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=9216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=9216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=9216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}