winutil/functions/microwin/Microwin-RemoveFeatures.ps1

83 lines
4.0 KiB
PowerShell
Raw Normal View History

function Microwin-RemoveFeatures() {
<#
.SYNOPSIS
Removes certain features from ISO image
[MicroWin] Preparation for 2025 (#3066) * Set Boot Manager entry timeout to 0 Fixes #2562 * Exclude Windows Hello stuff from package removal * Obscure passwords with Base64 and fix indentation Fixes #3064 * Fix name of excluded package * Update comment It reflects my feelings towards Microsoft when it comes to security a lot better * Remove jargon of scratch directory options * Package exclusion improvements - Removed AppX packages from OS package exclusion list - Added exclusion of PowerShell ISE (source: Discord server - yes, some people still use the PowerShell ISE) * Exclude Windows Photo Viewer from dir removal * Improve copy operation to Ventoy drives This change may fix the issues where there's a conflict between both Ventoy's and MicroWin's unattended answer files, causing target images to stop working as expected during OOBE * Add VirtIO functionality and more enhancements - Added the ability to grab VirtIO Guest Tools - Modified the description of the Copy ISO files function because it basically had nonsense * Fix typo (#3104) * Access specific property of ISO image object Only show the ISO path. No one is interested in the storage type * Add detections for expedited app removal They only affect 24H2 and newer. Earlier releases don't have these expedited apps * Update message * Add VirtIO instructions to MicroWin page * Add DISM command fallback This fallback is triggered if an exception occurs while getting information with the cmdlets (I couldn't test this on my host as everything magically works now - sometimes it threw the Class not registered error) * Exclude OpenSSH from package removal Some people need this to avoid installing third-party programs like PuTTY * Fixed some more indentation
2025-01-10 13:40:25 -06:00
.PARAMETER UseCmdlets
Determines whether or not to use the DISM cmdlets for processing.
- If true, DISM cmdlets will be used
- If false, calls to the DISM executable will be made whilst selecting bits and pieces from the output as a string (that was how MicroWin worked before
the DISM conversion to cmdlets)
.EXAMPLE
[MicroWin] Preparation for 2025 (#3066) * Set Boot Manager entry timeout to 0 Fixes #2562 * Exclude Windows Hello stuff from package removal * Obscure passwords with Base64 and fix indentation Fixes #3064 * Fix name of excluded package * Update comment It reflects my feelings towards Microsoft when it comes to security a lot better * Remove jargon of scratch directory options * Package exclusion improvements - Removed AppX packages from OS package exclusion list - Added exclusion of PowerShell ISE (source: Discord server - yes, some people still use the PowerShell ISE) * Exclude Windows Photo Viewer from dir removal * Improve copy operation to Ventoy drives This change may fix the issues where there's a conflict between both Ventoy's and MicroWin's unattended answer files, causing target images to stop working as expected during OOBE * Add VirtIO functionality and more enhancements - Added the ability to grab VirtIO Guest Tools - Modified the description of the Copy ISO files function because it basically had nonsense * Fix typo (#3104) * Access specific property of ISO image object Only show the ISO path. No one is interested in the storage type * Add detections for expedited app removal They only affect 24H2 and newer. Earlier releases don't have these expedited apps * Update message * Add VirtIO instructions to MicroWin page * Add DISM command fallback This fallback is triggered if an exception occurs while getting information with the cmdlets (I couldn't test this on my host as everything magically works now - sometimes it threw the Class not registered error) * Exclude OpenSSH from package removal Some people need this to avoid installing third-party programs like PuTTY * Fixed some more indentation
2025-01-10 13:40:25 -06:00
Microwin-RemoveFeatures -UseCmdlets $true
#>
[MicroWin] Preparation for 2025 (#3066) * Set Boot Manager entry timeout to 0 Fixes #2562 * Exclude Windows Hello stuff from package removal * Obscure passwords with Base64 and fix indentation Fixes #3064 * Fix name of excluded package * Update comment It reflects my feelings towards Microsoft when it comes to security a lot better * Remove jargon of scratch directory options * Package exclusion improvements - Removed AppX packages from OS package exclusion list - Added exclusion of PowerShell ISE (source: Discord server - yes, some people still use the PowerShell ISE) * Exclude Windows Photo Viewer from dir removal * Improve copy operation to Ventoy drives This change may fix the issues where there's a conflict between both Ventoy's and MicroWin's unattended answer files, causing target images to stop working as expected during OOBE * Add VirtIO functionality and more enhancements - Added the ability to grab VirtIO Guest Tools - Modified the description of the Copy ISO files function because it basically had nonsense * Fix typo (#3104) * Access specific property of ISO image object Only show the ISO path. No one is interested in the storage type * Add detections for expedited app removal They only affect 24H2 and newer. Earlier releases don't have these expedited apps * Update message * Add VirtIO instructions to MicroWin page * Add DISM command fallback This fallback is triggered if an exception occurs while getting information with the cmdlets (I couldn't test this on my host as everything magically works now - sometimes it threw the Class not registered error) * Exclude OpenSSH from package removal Some people need this to avoid installing third-party programs like PuTTY * Fixed some more indentation
2025-01-10 13:40:25 -06:00
param (
[Parameter(Mandatory = $true, Position = 0)] [bool]$UseCmdlets
)
try {
[MicroWin] Preparation for 2025 (#3066) * Set Boot Manager entry timeout to 0 Fixes #2562 * Exclude Windows Hello stuff from package removal * Obscure passwords with Base64 and fix indentation Fixes #3064 * Fix name of excluded package * Update comment It reflects my feelings towards Microsoft when it comes to security a lot better * Remove jargon of scratch directory options * Package exclusion improvements - Removed AppX packages from OS package exclusion list - Added exclusion of PowerShell ISE (source: Discord server - yes, some people still use the PowerShell ISE) * Exclude Windows Photo Viewer from dir removal * Improve copy operation to Ventoy drives This change may fix the issues where there's a conflict between both Ventoy's and MicroWin's unattended answer files, causing target images to stop working as expected during OOBE * Add VirtIO functionality and more enhancements - Added the ability to grab VirtIO Guest Tools - Modified the description of the Copy ISO files function because it basically had nonsense * Fix typo (#3104) * Access specific property of ISO image object Only show the ISO path. No one is interested in the storage type * Add detections for expedited app removal They only affect 24H2 and newer. Earlier releases don't have these expedited apps * Update message * Add VirtIO instructions to MicroWin page * Add DISM command fallback This fallback is triggered if an exception occurs while getting information with the cmdlets (I couldn't test this on my host as everything magically works now - sometimes it threw the Class not registered error) * Exclude OpenSSH from package removal Some people need this to avoid installing third-party programs like PuTTY * Fixed some more indentation
2025-01-10 13:40:25 -06:00
if ($UseCmdlets) {
$featlist = (Get-WindowsOptionalFeature -Path "$scratchDir")
[MicroWin] Preparation for 2025 (#3066) * Set Boot Manager entry timeout to 0 Fixes #2562 * Exclude Windows Hello stuff from package removal * Obscure passwords with Base64 and fix indentation Fixes #3064 * Fix name of excluded package * Update comment It reflects my feelings towards Microsoft when it comes to security a lot better * Remove jargon of scratch directory options * Package exclusion improvements - Removed AppX packages from OS package exclusion list - Added exclusion of PowerShell ISE (source: Discord server - yes, some people still use the PowerShell ISE) * Exclude Windows Photo Viewer from dir removal * Improve copy operation to Ventoy drives This change may fix the issues where there's a conflict between both Ventoy's and MicroWin's unattended answer files, causing target images to stop working as expected during OOBE * Add VirtIO functionality and more enhancements - Added the ability to grab VirtIO Guest Tools - Modified the description of the Copy ISO files function because it basically had nonsense * Fix typo (#3104) * Access specific property of ISO image object Only show the ISO path. No one is interested in the storage type * Add detections for expedited app removal They only affect 24H2 and newer. Earlier releases don't have these expedited apps * Update message * Add VirtIO instructions to MicroWin page * Add DISM command fallback This fallback is triggered if an exception occurs while getting information with the cmdlets (I couldn't test this on my host as everything magically works now - sometimes it threw the Class not registered error) * Exclude OpenSSH from package removal Some people need this to avoid installing third-party programs like PuTTY * Fixed some more indentation
2025-01-10 13:40:25 -06:00
$featlist = $featlist | Where-Object {
$_.FeatureName -NotLike "*Defender*" -AND
$_.FeatureName -NotLike "*Printing*" -AND
$_.FeatureName -NotLike "*TelnetClient*" -AND
$_.FeatureName -NotLike "*PowerShell*" -AND
$_.FeatureName -NotLike "*NetFx*" -AND
$_.FeatureName -NotLike "*Media*" -AND
$_.FeatureName -NotLike "*NFS*" -AND
$_.FeatureName -NotLike "*SearchEngine*" -AND
$_.FeatureName -NotLike "*RemoteDesktop*" -AND
$_.State -ne "Disabled"
}
} else {
$featList = dism /english /image="$scratchDir" /get-features | Select-String -Pattern "Feature Name : " -CaseSensitive -SimpleMatch
if ($?) {
$featList = $featList -split "Feature Name : " | Where-Object {$_}
# Exclude the same items. Note: for now, this doesn't exclude those features that are disabled.
# This will appear in the future
$featList = $featList | Where-Object {
$_ -NotLike "*Defender*" -AND
$_ -NotLike "*Printing*" -AND
$_ -NotLike "*TelnetClient*" -AND
$_ -NotLike "*PowerShell*" -AND
$_ -NotLike "*NetFx*" -AND
$_ -NotLike "*Media*" -AND
$_ -NotLike "*NFS*" -AND
$_ -NotLike "*SearchEngine*" -AND
$_ -NotLike "*RemoteDesktop*"
}
} else {
Write-Host "Features could not be obtained with DISM. MicroWin processing will continue, but features will be skipped."
return
}
}
[MicroWin] Preparation for 2025 (#3066) * Set Boot Manager entry timeout to 0 Fixes #2562 * Exclude Windows Hello stuff from package removal * Obscure passwords with Base64 and fix indentation Fixes #3064 * Fix name of excluded package * Update comment It reflects my feelings towards Microsoft when it comes to security a lot better * Remove jargon of scratch directory options * Package exclusion improvements - Removed AppX packages from OS package exclusion list - Added exclusion of PowerShell ISE (source: Discord server - yes, some people still use the PowerShell ISE) * Exclude Windows Photo Viewer from dir removal * Improve copy operation to Ventoy drives This change may fix the issues where there's a conflict between both Ventoy's and MicroWin's unattended answer files, causing target images to stop working as expected during OOBE * Add VirtIO functionality and more enhancements - Added the ability to grab VirtIO Guest Tools - Modified the description of the Copy ISO files function because it basically had nonsense * Fix typo (#3104) * Access specific property of ISO image object Only show the ISO path. No one is interested in the storage type * Add detections for expedited app removal They only affect 24H2 and newer. Earlier releases don't have these expedited apps * Update message * Add VirtIO instructions to MicroWin page * Add DISM command fallback This fallback is triggered if an exception occurs while getting information with the cmdlets (I couldn't test this on my host as everything magically works now - sometimes it threw the Class not registered error) * Exclude OpenSSH from package removal Some people need this to avoid installing third-party programs like PuTTY * Fixed some more indentation
2025-01-10 13:40:25 -06:00
if ($UseCmdlets) {
foreach ($feature in $featList) {
$status = "Removing feature $($feature.FeatureName)"
Write-Progress -Activity "Removing features" -Status $status -PercentComplete ($counter++/$featlist.Count*100)
Write-Debug "Removing feature $($feature.FeatureName)"
Disable-WindowsOptionalFeature -Path "$scratchDir" -FeatureName $($feature.FeatureName) -Remove -ErrorAction SilentlyContinue -NoRestart
}
} else {
foreach ($feature in $featList) {
$status = "Removing feature $feature"
Write-Progress -Activity "Removing features" -Status $status -PercentComplete ($counter++/$featlist.Count*100)
Write-Debug "Removing feature $feature"
dism /english /image="$scratchDir" /disable-feature /featurename=$feature /remove /quiet /norestart | Out-Null
if ($? -eq $false) {
Write-Host "Feature $feature could not be disabled."
}
}
}
Write-Progress -Activity "Removing features" -Status "Ready" -Completed
Write-Host "You can re-enable the disabled features at any time, using either Windows Update or the SxS folder in <installation media>\Sources."
} catch {
[MicroWin] Preparation for 2025 (#3066) * Set Boot Manager entry timeout to 0 Fixes #2562 * Exclude Windows Hello stuff from package removal * Obscure passwords with Base64 and fix indentation Fixes #3064 * Fix name of excluded package * Update comment It reflects my feelings towards Microsoft when it comes to security a lot better * Remove jargon of scratch directory options * Package exclusion improvements - Removed AppX packages from OS package exclusion list - Added exclusion of PowerShell ISE (source: Discord server - yes, some people still use the PowerShell ISE) * Exclude Windows Photo Viewer from dir removal * Improve copy operation to Ventoy drives This change may fix the issues where there's a conflict between both Ventoy's and MicroWin's unattended answer files, causing target images to stop working as expected during OOBE * Add VirtIO functionality and more enhancements - Added the ability to grab VirtIO Guest Tools - Modified the description of the Copy ISO files function because it basically had nonsense * Fix typo (#3104) * Access specific property of ISO image object Only show the ISO path. No one is interested in the storage type * Add detections for expedited app removal They only affect 24H2 and newer. Earlier releases don't have these expedited apps * Update message * Add VirtIO instructions to MicroWin page * Add DISM command fallback This fallback is triggered if an exception occurs while getting information with the cmdlets (I couldn't test this on my host as everything magically works now - sometimes it threw the Class not registered error) * Exclude OpenSSH from package removal Some people need this to avoid installing third-party programs like PuTTY * Fixed some more indentation
2025-01-10 13:40:25 -06:00
Write-Host "Unable to get information about the features. A fallback will be used..."
Write-Host "Error information: $($_.Exception.Message)" -ForegroundColor Yellow
[MicroWin] Preparation for 2025 (#3066) * Set Boot Manager entry timeout to 0 Fixes #2562 * Exclude Windows Hello stuff from package removal * Obscure passwords with Base64 and fix indentation Fixes #3064 * Fix name of excluded package * Update comment It reflects my feelings towards Microsoft when it comes to security a lot better * Remove jargon of scratch directory options * Package exclusion improvements - Removed AppX packages from OS package exclusion list - Added exclusion of PowerShell ISE (source: Discord server - yes, some people still use the PowerShell ISE) * Exclude Windows Photo Viewer from dir removal * Improve copy operation to Ventoy drives This change may fix the issues where there's a conflict between both Ventoy's and MicroWin's unattended answer files, causing target images to stop working as expected during OOBE * Add VirtIO functionality and more enhancements - Added the ability to grab VirtIO Guest Tools - Modified the description of the Copy ISO files function because it basically had nonsense * Fix typo (#3104) * Access specific property of ISO image object Only show the ISO path. No one is interested in the storage type * Add detections for expedited app removal They only affect 24H2 and newer. Earlier releases don't have these expedited apps * Update message * Add VirtIO instructions to MicroWin page * Add DISM command fallback This fallback is triggered if an exception occurs while getting information with the cmdlets (I couldn't test this on my host as everything magically works now - sometimes it threw the Class not registered error) * Exclude OpenSSH from package removal Some people need this to avoid installing third-party programs like PuTTY * Fixed some more indentation
2025-01-10 13:40:25 -06:00
Microwin-RemoveFeatures -UseCmdlets $false
}
}