|
| 1 | +#Requires -Modules @{ModuleName="OZO";ModuleVersion="1.4.0"},@{ModuleName="OZOLogger";ModuleVersion="1.1.0"} -RunAsAdministrator |
| 2 | + |
| 3 | +<#PSScriptInfo |
| 4 | + .VERSION 1.0.0 |
| 5 | + .GUID 26313204-248b-4816-89de-4c605134b2ca |
| 6 | + .AUTHOR Andy Lievetz <[email protected]> |
| 7 | + .COMPANYNAME One Zero One |
| 8 | + .COPYRIGHT This script is released under the terms of the GNU General Public License ("GPL") version 2.0. |
| 9 | + .TAGS |
| 10 | + .LICENSEURI https://github.com/onezeroone-dev/OZO-Remove-AppX-Packages/blob/main/LICENSE |
| 11 | + .PROJECTURI https://github.com/onezeroone-dev/OZO-Remove-AppX-Packages |
| 12 | + .ICONURI |
| 13 | + .EXTERNALMODULEDEPENDENCIES |
| 14 | + .REQUIREDSCRIPTS |
| 15 | + .EXTERNALSCRIPTDEPENDENCIES |
| 16 | + .RELEASENOTES https://github.com/onezeroone-dev/OZO-Remove-AppX-Packages/blob/main/CHANGELOG.md |
| 17 | +#> |
| 18 | + |
| 19 | +<# |
| 20 | + .DESCRIPTION |
| 21 | + Removes provisioned AppX packages for the current user and available AppX packages from the running system. |
| 22 | + .PARAMETER Packages |
| 23 | + A comma-separated list of packages to remove. |
| 24 | + .LINK |
| 25 | + https://github.com/onezeroone-dev/OZO-Remove-AppX-Packages/blob/main/README.md |
| 26 | + .NOTES |
| 27 | + Run this script in an Administrator PowerShell. |
| 28 | +#> |
| 29 | +[CmdLetBinding(SupportsShouldProcess=$true)] |
| 30 | +Param( |
| 31 | + [Parameter(Mandatory=$true,HelpMessage="A comma-separated list of packages to remove")][Array] $Packages |
| 32 | +) |
| 33 | + |
| 34 | +Function Get-OZOUserInteractive { |
| 35 | + return [Environment]::UserInteractive |
| 36 | +} |
| 37 | + |
| 38 | +# MAIN |
| 39 | +# Variables |
| 40 | +[Array] $AppxPackages = (Get-AppxPackage) |
| 41 | +[Array] $AppxProvisionedPackages = (Get-AppxProvisionedPackage -Online) |
| 42 | +# Determine if the session is user-interactive |
| 43 | +If ((Get-OZOUserInteractive) -eq $true) { |
| 44 | + # Session is user-interactive; iterate through the list of packages |
| 45 | + ForEach ($Package in $Packages) { |
| 46 | + Write-Host ("Processing available Appx packages.") |
| 47 | + # Determine if the AppxPackages array contains the package |
| 48 | + If (($AppxPackages).Name -Contains $Package) { |
| 49 | + # The array contains the package; remove it |
| 50 | + Try { |
| 51 | + Remove-AppxPackage -Package ($AppxPackages | Where-Object {$_.Name -eq $Package}).PackageFullName -ErrorAction Stop |
| 52 | + # Success |
| 53 | + Write-Host ("Removed the " + $Package + " available package.") |
| 54 | + } Catch { |
| 55 | + # Failure |
| 56 | + Write-Host ("Error removing the " + $Package + "available package. Error message is: " + $_) |
| 57 | + } |
| 58 | + } Else { |
| 59 | + Write-Host ("Did not find " + $Package + " among the available Appx packages.") |
| 60 | + } |
| 61 | + Write-Host ("Processing provisioned Appx packages.") |
| 62 | + # Determine if the AppxProvisionedPackages array contains the package |
| 63 | + If (($AppxProvisionedPackages).DisplayName -Contains $Package) { |
| 64 | + # The array contains the package; remove it |
| 65 | + Try { |
| 66 | + Remove-AppxProvisionedPackage -Online -PackageName ($AppxProvisionedPackages | Where-Object {$_.DisplayName -eq $Package}).PackageName | Out-Null |
| 67 | + Write-Host ("Removed the " + $Package + " provisioned package.") |
| 68 | + } Catch { |
| 69 | + Write-Host ("Error removing the " + $Package + "provisioned package. Error message is: " + $_) |
| 70 | + } |
| 71 | + } Else { |
| 72 | + Write-Host ("Did not find " + $Package + " among the provisioned Appx packages.") |
| 73 | + } |
| 74 | + } |
| 75 | +} Else { |
| 76 | + # Session is not user-interactive |
| 77 | + Write-OZOProvider -Message "Please run this script in a user-interactive session." -Level "Error" |
| 78 | +} |
0 commit comments