#get apps to deprovision for file appx.txt that has to be in same folder as this script.
#apps listed in the appx.txt need to be listed as PackageFamilyNames
Write-Host "--Get Removable AppXs to deprovision--"
$appXs = get-content "$PSScriptRoot\appx.txt"
$appXs
Write-host *********************`n
Write-Host "--need SIDs for local accounts and SYSTEM to remove non-removable APPX--"
Write-Host "--Existing Profile SIDs for logged on user accounts are requiored to remove non-removable APPX also, but will be calculated dynamically when applying EOL paths--"
Write-Host "getting defaultuser0 SID and setting SYSTEM SID and EOL PATH"
$default_sid = (get-localuser -name "defaultuser0").SID
Write-Host "defaultuser0 SID is $default_sid"
$system_sid = "S-1-5-18"
Write-Host "SYSTEM SID is $system_sid"
$EOLPATH = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\EndOfLife"
Write-Host "EOL APPX Registry Path is $EOLPATH"
Write-Host "---Done Collecting Info and Setting Variables---"
Write-host *********************`n
Write-Host "---Uninstalling AppXs---"
ForEach ($appX in $appXs) {
Write-Host "Current App $appX"
Write-Host "Checking if App is currently installed"
$appX_trim = $appX.Substring(0,$appX.IndexOf("_"))
$curr_appX = Get-AppxPackage -AllUsers -Name $appX_trim
if ($curr_appX) {
Write-Host "$appX_trim installed, removing now..."
$NonRemovable = ($curr_appX).NonRemovable
$PackageFullName = ($curr_appX).PackageFullName #name not matching the registry, need to wildcard
$inbox_path = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Appx\AppxAllUserStore\InboxApplications\"
$profile_path = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\"
If ($NonRemovable -eq $TRUE) {
Write-Host "$appX_trim is marked an NonRemovable... Attempting to allow removal"
Write-Host "Writing EOL keys for system and defaultuser0"
Write-Host "DEFAULTUSER0 EOL: $EOLPATH\$default_sid\$PackageFullName"
New-Item -Path "$EOLPATH\$default_sid\$PackageFullName" -Force
Write-Host "SYSTEM EOL PATH: $EOLPATH\$system_sid\$PackageFullName0"
New-Item -Path "$EOLPATH\$system_sid\$PackageFullName" -Force
Write-Host "detecting logged on profiles for app removal EOL"
$ProfileList = Get-ChildItem -Path $profile_path -Recurse -ErrorAction SilentlyContinue -Exclude "S-1-5-*"
Write-Host "Profile List:"
$ProfileList.Name
ForEach ($Profile in $ProfileList) {
$userSID = $Profile.PSChildName
New-Item -Path "$EOLPATH\$userSID\$PackageFullName" -Force
}
Write-Host "locating inbox path for $appX_trim..."
$inbox_keys = Get-ChildItem -Path $inbox_path -Recurse -Include "$appX_trim*" -ErrorAction SilentlyContinue
if ($inbox_keys) {
ForEach ($key in $inbox_keys) {
write-host "cleanup app returning - deleting $key"
& REG DELETE "$key" /f
}
}
}
if (Get-AppxProvisionedPackage -Online | Where-object {$_.DisplayName -Match $appX_trim}) {Get-AppxProvisionedPackage -Online | Where-object {$_.DisplayName -Match $appX_trim} | Remove-AppxProvisionedPackage -online -AllUsers}
if (Get-AppxProvisionedPackage -Online | Where-object {$_.DisplayName -Match $appX_trim}) {Get-AppxProvisionedPackage -Online | Where-object {$_.DisplayName -Match $appX_trim} | Remove-AppxProvisionedPackage -online}
if (Get-AppxProvisionedPackage -Online | Where-object {$_.DisplayName -Match $appX_trim}) {Write-Host "Failed to deprovision $appX_trim"} ELSE {Write-Host "$appX deprovisioned successfully"}
if (Get-AppxPackage -AllUsers -PackageTypeFilter Bundle -Name $appX_trim) {Get-AppxPackage -AllUsers -PackageTypeFilter Bundle -Name $appX_trim | Remove-AppxPackage -AllUsers}
if (Get-AppxPackage -AllUsers -PackageTypeFilter Bundle -Name $appX_trim) {Get-AppxPackage -AllUsers -PackageTypeFilter Bundle -Name $appX_trim | Remove-AppxPackage} #seems to like a second pass with allusers to get the system account removed
if (Get-AppxPackage -AllUsers -Name $appX_trim) {Get-AppxPackage -AllUsers -Name $appX_trim | Remove-AppxPackage -AllUsers}
if (Get-AppxPackage -AllUsers -Name $appX_trim) {Get-AppxPackage -AllUsers -Name $appX_trim | Remove-AppxPackage} #seems to like a second pass with allusers to get the system account removed
if (Get-AppxPackage -AllUsers -PackageTypeFilter Bundle -Name $appX_trim) {Write-Host "Failed to remove $appX_trim as bundle"} ELSE {Write-Host "$appX_trim no longer detected as bundle"}
if (Get-AppxPackage -AllUsers -Name $appX_trim) {Write-Host "Failed to remove $appX_trim outside bundle"} ELSE {Write-Host "$appX_trim no longer detected outside bundle"}
} ELSE {
Write-Host "$appX_trim NOT installed, moving on..."
}
Write-host *********************`n
}