-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteWindowsAppBundle.ps1
More file actions
79 lines (68 loc) · 3.05 KB
/
DeleteWindowsAppBundle.ps1
File metadata and controls
79 lines (68 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<#
=========================================================
Script: DeleteWindowsAppBundle.ps1
Purpose: Remove all Appx packages matching a name or
wildcard, then purge their
WindowsApps folders.
Usage: Run as Administrator and not system-level
=========================================================
#>
### Configurable variables ###
$appPackageName = "<AppBundle Name Here>"
# ← Can be a wildcard (e.g. "*OneNote*") or a full name
#$appPackageName = "Microsoft.MicrosoftOfficeHub_18.1903.1152.0_x64__8wekyb3d8bbwe"
#$appPackageName = "Microsoft.MicrosoftOfficeHub*"
#$appPackageName = "*OfficeHub*"
$windowsAppsBasePath = "C:\Program Files\WindowsApps"
$takeownPath = "$env:SystemRoot\system32\takeown.exe"
$icaclsPath = "$env:SystemRoot\system32\icacls.exe"
### Begin Script ###
$ErrorActionPreference = "Continue"
Write-Host "=== Removing Appx packages matching '$appPackageName' ==="
# 1) Remove all matching AppxPackage entries
$packages = Get-AppxPackage -AllUsers -Name $appPackageName -ErrorAction SilentlyContinue
if (-not $packages) {
Write-Host "No Appx packages found matching '$appPackageName'." -ForegroundColor Yellow
} else {
foreach ($pkg in $packages) {
try {
Remove-AppxPackage -Package $pkg.PackageFullName -AllUsers -ErrorAction Stop
Write-Host "Removed package: '$($pkg.PackageFullName)'" -ForegroundColor Green
}
catch [System.TypeInitializationException] {
Write-Host "Ignored TypeInitializationException for '$($pkg.PackageFullName)'" -ForegroundColor Yellow
}
catch {
Write-Host "ERROR removing '$($pkg.PackageFullName)': $_" -ForegroundColor Red
}
}
}
# 2) Verify no Appx packages remain
$appCheck = Get-AppxPackage -AllUsers -Name $appPackageName -ErrorAction SilentlyContinue
if ($appCheck) {
Write-Host "Some Appx packages still present after removal." -ForegroundColor Red
} else {
Write-Host "All matching Appx packages removed." -ForegroundColor Green
}
Write-Host "`n=== Manual cleanup of WindowsApps folders ==="
# 3) Find all WindowsApps subfolders matching the same pattern
$folders = Get-ChildItem -Path $windowsAppsBasePath -Directory -ErrorAction SilentlyContinue |
Where-Object { $_.Name -like $appPackageName }
if (-not $folders) {
Write-Host "No residual WindowsApps folders matching '$appPackageName'." -ForegroundColor Yellow
} else {
foreach ($folder in $folders) {
$appPath = $folder.FullName
Write-Host "Cleaning up folder: '$appPath'"
try {
& $takeownPath /F $appPath /R /D Y | Out-Null
& $icaclsPath $appPath /grant Administrators:F /T | Out-Null
Remove-Item -Path $appPath -Recurse -Force -ErrorAction Stop
Write-Host "Deleted: '$appPath'" -ForegroundColor Green
}
catch {
Write-Host "Failed to delete '$appPath': $_" -ForegroundColor Red
}
}
}
Write-Host "`n=== Script complete! ===" -ForegroundColor Cyan