Classes/AppxPackageManager.ps1
|
class AppxPackageManager { [LibraryContext]$Context AppxPackageManager([LibraryContext]$context) { $this.Context = $context } [void]Uninstall([string]$singleSoftware, [object]$softwareList, [bool]$permanently) { if ($softwareList) { foreach ($softwareName in $softwareList) { $this.Uninstall($softwareName, $null, $permanently) } } elseif ($singleSoftware) { if ($permanently) { $this.Context.LogManager.Log("Checking for PERMANENTLY installed apps matching: $singleSoftware") } else { $this.Context.LogManager.Log("Checking for USER installed apps matching: $singleSoftware") } if ($permanently) { $installedApps = Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -like "*$singleSoftware*" } } else { $installedApps = Get-AppxPackage | Where-Object { $_.Name -like "*$singleSoftware*" } } if ($installedApps.Count -eq 0) { $this.Context.LogManager.Log("No installed apps matching $singleSoftware were found", "WARN") } elseif ($installedApps.Count -ge 10) { $this.Context.LogManager.Log("Found $($installedApps.Count) app(s) matching $singleSoftware => This is too much, skipping!", "ERROR") } else { $this.Context.LogManager.Log("Found $($installedApps.Count) app(s) matching $singleSoftware => Attempting to remove them...") foreach ($installedApp in $installedApps) { if ($permanently) { $this.Context.LogManager.Log("Removing (PERMANENT) app: $($installedApp.DisplayName)") try { Remove-AppxProvisionedPackage -online -PackageName $installedApp.PackageName -ErrorAction SilentlyContinue } catch { $this.Context.LogManager.Log("Could not remove (PERMANENT) app: $($installedApp.DisplayName)", "ERROR") } # Recheck if the app was removed $remainingApps = Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -like "*$singleSoftware*" } $remainingNames = $remainingApps | ForEach-Object { $_.DisplayName } | Sort-Object } else { $this.Context.LogManager.Log("Removing (USER) app: $($installedApp.Name)") $installedApp | Remove-AppxPackage -ErrorAction SilentlyContinue # Recheck if the app was removed $remainingApps = Get-AppxPackage | Where-Object { $_.Name -like "*$singleSoftware*" } $remainingNames = $remainingApps | ForEach-Object { $_.Name } | Sort-Object } if ($remainingNames.Count -gt 0) { $this.Context.LogManager.Log("The following app(s) could NOT be removed: $($remainingNames -join '; ')", "ERROR") } else { $this.Context.LogManager.Log("Successfully removed app(s) matching: $singleSoftware") } } } } } } |