# Functions
function Write-LogEntry {
param(
[parameter(Mandatory=$true, HelpMessage="Value added to the RemovedApps.log file.")]
[ValidateNotNullOrEmpty()]
[string]$Value,
[parameter(Mandatory=$false, HelpMessage="Name of the log file that the entry will written to.")]
[ValidateNotNullOrEmpty()]
[string]$FileName = "RemovedApps.log"
)
# Determine log file location
$LogFilePath = Join-Path -Path $env:windir -ChildPath "Temp\$($FileName)"
# Add value to log file
try {
Add-Content -Value $Value -LiteralPath $LogFilePath -ErrorAction Stop
}
catch [System.Exception] {
Write-Warning -Message "Unable to append log entry to RemovedApps.log file"
}
}
# Get a list of all apps
Write-LogEntry -Value "Starting appx package removal"
$AppArrayList = Get-AppxPackage -PackageTypeFilter Bundle -AllUsers | Select-Object -Property Name, PackageFullName | Sort-Object -Property Name
# White list of appx packages to keep installed
$WhiteListedApps = @(
"Microsoft.DesktopAppInstaller",
"Microsoft.StorePurchaseApp"
"Microsoft.WindowsCalculator",
"Microsoft.WindowsStore",
"Microsoft.XboxApp",
"Microsoft.Windows.Photos",
"Microsoft.StorePurchaseApp",
"Windows.PrintDialog",
"Microsoft.XboxIdentityProvider",
"Microsoft.Windows.ContentDeliveryManager",
"windows.immersivecontrolpanel",
"Microsoft.MicrosoftEdge",
"Microsoft.MSPaint",
"Microsoft.Windows.SecHealthUI",
"Microsoft.WindowsCamera",
"Microsoft.Wallet",
"Microsoft.XboxGameOverlay",
"Microsoft.XboxSpeechToTextOverlay",
"Microsoft.XboxGameCallableui",
"Microsoft.WindowsFeedbackHub"
)
# Loop through the list of appx packages
foreach ($App in $AppArrayList) {
# If application name not in appx package white list, remove AppxPackage and AppxProvisioningPackage
if (($App.Name -in $WhiteListedApps)) {
Write-LogEntry -Value "Skipping excluded application package: $($App.Name)"
}
else {
# Gather package names
$AppPackageFullName = Get-AppxPackage -Name $App.Name | Select-Object -ExpandProperty PackageFullName
$AppProvisioningPackageName = Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -like $App.Name } | Select-Object -ExpandProperty PackageName
# Attempt to remove AppxPackage
try {
Write-LogEntry -Value "Removing application package: $($AppPackageFullName)"
Remove-AppxPackage -Package $AppPackageFullName -ErrorAction Stop
}
catch [System.Exception] {
Write-Warning -Message $_.Exception.Message
}
# Attempt to remove AppxProvisioningPackage
if ($AppProvisioningPackageName -ne $null) {
try {
Write-LogEntry -Value "Removing application provisioning package: $($AppProvisioningPackageName)"
Remove-AppxProvisionedPackage -PackageName $AppProvisioningPackageName -Online -ErrorAction Stop
}
catch [System.Exception] {
Write-Warning -Message $_.Exception.Message
}
}
}
}
# White list of Features On Demand V2 packages
Write-LogEntry -Value "Starting Features on Demand V2 removal"
$WhiteListOnDemand = "NetFX3|Tools.Graphics.DirectX|Tools.DeveloperMode.Core|Language|Browser.InternetExplorer|Media.WindowsMediaPlayer"
# Get Features On Demand that should be removed
$OnDemandFeatures = Get-WindowsCapability -Online | Where-Object { $_.Name -notmatch $WhiteListOnDemand -and $_.State -like "Installed"} | Select-Object -ExpandProperty Name
foreach ($Feature in $OnDemandFeatures) {
try {
Write-LogEntry -Value "Removing feature on demand: $($Feature)"
Get-WindowsCapability -Online -ErrorAction Stop | Where-Object { $_.Name -like $Feature } | Remove-WindowsCapability -Online -ErrorAction Stop
}
catch [System.Exception] {
Write-Warning -Message $_.Exception.Message
}
}