Skip to content

Commit

Permalink
Merge pull request #5 from itigoag/develop
Browse files Browse the repository at this point in the history
quick fix
  • Loading branch information
sbaerlocher authored Jun 18, 2019
2 parents 001af81 + 9ef6d55 commit da5e296
Showing 1 changed file with 146 additions and 110 deletions.
256 changes: 146 additions & 110 deletions scripts/Invoke-RemoveBuiltinApps.ps1
Original file line number Diff line number Diff line change
@@ -1,133 +1,169 @@
# 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 {
Out-File -InputObject $Value -Append -NoClobber -Encoding Default -FilePath $LogFilePath -ErrorAction Stop
}
catch [System.Exception] {
Write-Warning -Message "Unable to append log entry to RemovedApps.log file"
}
Begin {
# White list of Features On Demand V2 packages
$WhiteListOnDemand = "NetFX3|Tools.Graphics.DirectX|Tools.DeveloperMode.Core|Language|Browser.InternetExplorer|ContactSupport|OneCoreUAP|Media.WindowsMediaPlayer|Hello.Face"

# White list of appx packages to keep installed
$WhiteListedApps = New-Object -TypeName System.Collections.ArrayList
$WhiteListedApps.AddRange(@(
# "Microsoft.DesktopAppInstaller",
# "Microsoft.Messaging",
"Microsoft.MSPaint",
"Microsoft.Windows.Photos",
# "Microsoft.StorePurchaseApp",
# "Microsoft.MicrosoftOfficeHub",
"Microsoft.MicrosoftStickyNotes",
"Microsoft.WindowsAlarms",
"Microsoft.WindowsCalculator",
# "Microsoft.WindowsCommunicationsApps", # Mail, Calendar etc
"Microsoft.WindowsSoundRecorder",
"Microsoft.WindowsStore"
))

# Windows 10 version 1809
$WhiteListedApps.AddRange(@(
"Microsoft.ScreenSketch",
"Microsoft.HEIFImageExtension",
"Microsoft.VP9VideoExtensions",
"Microsoft.WebMediaExtensions",
"Microsoft.WebpImageExtension"
))

# Windows 10 version 1903
# No new apps
}
Process {
# 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)"

# Get a list of all apps
Write-LogEntry -Value "Starting built-in AppxPackage, AppxProvisioningPackage and Feature on Demand V2 removal process"
$AppArrayList = Get-AppxPackage -PackageTypeFilter Bundle -AllUsers | Select-Object -Property Name, PackageFullName | Sort-Object -Property Name

# White list of appx packages to keep installed
$WhiteListedApps = New-Object -TypeName System.Collections.ArrayList
$WhiteListedApps.AddRange(@(
# "Microsoft.DesktopAppInstaller",
# "Microsoft.Messaging",
"Microsoft.MSPaint",
"Microsoft.Windows.Photos",
# "Microsoft.StorePurchaseApp",
# "Microsoft.MicrosoftOfficeHub",
"Microsoft.MicrosoftStickyNotes",
"Microsoft.WindowsAlarms",
"Microsoft.WindowsCalculator",
# "Microsoft.WindowsCommunicationsApps", # Mail, Calendar etc
"Microsoft.WindowsSoundRecorder",
"Microsoft.WindowsStore"
))

# Windows 10 version 1809
$WhiteListedApps.AddRange(@(
"Microsoft.ScreenSketch",
"Microsoft.HEIFImageExtension",
"Microsoft.VP9VideoExtensions",
"Microsoft.WebMediaExtensions",
"Microsoft.WebpImageExtension"
))

# 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)"
# Add value to log file
try {
Out-File -InputObject $Value -Append -NoClobber -Encoding Default -FilePath $LogFilePath -ErrorAction Stop
}
catch [System.Exception] {
Write-Warning -Message "Unable to append log entry to RemovedApps.log file"
}
}
else {
# Gather package names
$AppPackageFullName = Get-AppxPackage -Name $App.Name | Select-Object -ExpandProperty PackageFullName -First 1
$AppProvisioningPackageName = Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -like $App.Name } | Select-Object -ExpandProperty PackageName -First 1

# Attempt to remove AppxPackage
if ($AppPackageFullName -ne $null) {
try {
Write-LogEntry -Value "Removing AppxPackage: $($AppPackageFullName)"
Remove-AppxPackage -Package $AppPackageFullName -ErrorAction Stop | Out-Null

function Test-RegistryValue {
param(
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$Path,

[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$Name
)
# If item property value exists return True, else catch the failure and return False
try {
if ($PSBoundParameters["Name"]) {
$Existence = Get-ItemProperty -Path $Path | Select-Object -ExpandProperty $Name -ErrorAction Stop
}
else {
$Existence = Get-ItemProperty -Path $Path -ErrorAction Stop
}
catch [System.Exception] {
Write-LogEntry -Value "Removing AppxPackage '$($AppPackageFullName)' failed: $($_.Exception.Message)"

if ($Existence -ne $null) {
return $true
}
}
else {
Write-LogEntry -Value "Unable to locate AppxPackage: $($AppPackageFullName)"
catch [System.Exception] {
return $false
}
}

function Set-RegistryValue {
param(
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$Path,

[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$Name,

# Attempt to remove AppxProvisioningPackage
if ($AppProvisioningPackageName -ne $null) {
try {
Write-LogEntry -Value "Removing AppxProvisioningPackage: $($AppProvisioningPackageName)"
Remove-AppxProvisionedPackage -PackageName $AppProvisioningPackageName -Online -ErrorAction Stop | Out-Null
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$Value,

[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[ValidateSet("DWORD", "String")]
[string]$Type
)
try {
$RegistryValue = Get-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue
if ($RegistryValue -ne $null) {
Set-ItemProperty -Path $Path -Name $Name -Value $Value -Force -ErrorAction Stop
}
catch [System.Exception] {
Write-LogEntry -Value "Removing AppxProvisioningPackage '$($AppProvisioningPackageName)' failed: $($_.Exception.Message)"
else {
New-ItemProperty -Path $Path -Name $Name -PropertyType $Type -Value $Value -Force -ErrorAction Stop | Out-Null
}
}
else {
Write-LogEntry -Value "Unable to locate AppxProvisioningPackage: $($AppProvisioningPackageName)"
catch [System.Exception] {
Write-Warning -Message "Failed to create or update registry value '$($Name)' in '$($Path)'. Error message: $($_.Exception.Message)"
}
}
}

# White list of Features On Demand V2 packages
Write-LogEntry -Value "Starting Features on Demand V2 removal process"
$WhiteListOnDemand = "NetFX3|Tools.Graphics.DirectX|Tools.DeveloperMode.Core|Language|Browser.InternetExplorer|ContactSupport|OneCoreUAP|Media.WindowsMediaPlayer"
# Initial logging
Write-LogEntry -Value "Starting built-in AppxPackage, AppxProvisioningPackage and Feature on Demand V2 removal process"

# Get Features On Demand that should be removed
try {
$OSBuildNumber = Get-WmiObject -Class "Win32_OperatingSystem" | Select-Object -ExpandProperty BuildNumber
# Determine provisioned apps
$AppArrayList = Get-AppxProvisionedPackage -Online | Select-Object -ExpandProperty DisplayName

# Handle cmdlet limitations for older OS builds
if ($OSBuildNumber -le "16299") {
$OnDemandFeatures = Get-WindowsCapability -Online -ErrorAction Stop | Where-Object { $_.Name -notmatch $WhiteListOnDemand -and $_.State -like "Installed"} | Select-Object -ExpandProperty Name
}
else {
$OnDemandFeatures = Get-WindowsCapability -Online -LimitAccess -ErrorAction Stop | Where-Object { $_.Name -notmatch $WhiteListOnDemand -and $_.State -like "Installed"} | Select-Object -ExpandProperty Name
}
# Loop through the list of appx packages
foreach ($App in $AppArrayList) {
Write-LogEntry -Value "Processing appx package: $($App)"

foreach ($Feature in $OnDemandFeatures) {
try {
Write-LogEntry -Value "Removing Feature on Demand V2 package: $($Feature)"
# If application name not in appx package white list, remove AppxPackage and AppxProvisioningPackage
if (($App -in $WhiteListedApps)) {
Write-LogEntry -Value "Skipping excluded application package: $($App)"
}
else {
# Gather package names
$AppPackageFullName = Get-AppxPackage -Name $App | Select-Object -ExpandProperty PackageFullName -First 1
$AppProvisioningPackageName = Get-AppxProvisionedPackage -Online | Where-Object { $_.DisplayName -like $App } | Select-Object -ExpandProperty PackageName -First 1

# Handle cmdlet limitations for older OS builds
if ($OSBuildNumber -le "16299") {
Get-WindowsCapability -Online -ErrorAction Stop | Where-Object { $_.Name -like $Feature } | Remove-WindowsCapability -Online -ErrorAction Stop | Out-Null
# Attempt to remove AppxPackage
if ($AppPackageFullName -ne $null) {
try {
Write-LogEntry -Value "Removing AppxPackage: $($AppPackageFullName)"
Remove-AppxPackage -Package $AppPackageFullName -ErrorAction Stop | Out-Null
}
catch [System.Exception] {
Write-LogEntry -Value "Removing AppxPackage '$($AppPackageFullName)' failed: $($_.Exception.Message)"
}
}
else {
Get-WindowsCapability -Online -LimitAccess -ErrorAction Stop | Where-Object { $_.Name -like $Feature } | Remove-WindowsCapability -Online -ErrorAction Stop | Out-Null
Write-LogEntry -Value "Unable to locate AppxPackage: $($AppPackageFullName)"
}

# Attempt to remove AppxProvisioningPackage
if ($AppProvisioningPackageName -ne $null) {
try {
Write-LogEntry -Value "Removing AppxProvisioningPackage: $($AppProvisioningPackageName)"
Remove-AppxProvisionedPackage -PackageName $AppProvisioningPackageName -Online -ErrorAction Stop | Out-Null
}
catch [System.Exception] {
Write-LogEntry -Value "Removing AppxProvisioningPackage '$($AppProvisioningPackageName)' failed: $($_.Exception.Message)"
}
}
else {
Write-LogEntry -Value "Unable to locate AppxProvisioningPackage: $($AppProvisioningPackageName)"
}
}
catch [System.Exception] {
Write-LogEntry -Value "Removing Feature on Demand V2 package failed: $($_.Exception.Message)"
}
}
}
catch [System.Exception] {
Write-LogEntry -Value "Attempting to list Feature on Demand V2 packages failed: $($_.Exception.Message)"
}
}

# Complete
Write-LogEntry -Value "Completed built-in AppxPackage, AppxProvisioningPackage and Feature on Demand V2 removal process"

}

0 comments on commit da5e296

Please sign in to comment.