/
Invoke-RemoveBuiltinApps.ps1
87 lines (77 loc) · 3.47 KB
/
Invoke-RemoveBuiltinApps.ps1
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
80
81
82
83
84
85
86
87
# 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.Messaging",
"Microsoft.StorePurchaseApp"
"Microsoft.WindowsCalculator",
"Microsoft.WindowsCommunicationsApps",
"Microsoft.WindowsSoundRecorder",
"Microsoft.WindowsStore"
)
# 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"
# 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
}
}