Skip to content

Commit

Permalink
refactor: add lib to uninstall/install optional features
Browse files Browse the repository at this point in the history
- Optimized code from optimize-windows-features
- There are to switches to the function: `-Disabled` or `-Enabled`
- Also disable Internet Explorer from Optional Features
  • Loading branch information
LeDragoX committed May 19, 2022
1 parent fcf0224 commit f8ae489
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 52 deletions.
83 changes: 83 additions & 0 deletions src/lib/set-windows-feature-state.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Import-Module -DisableNameChecking $PSScriptRoot\..\lib\"title-templates.psm1"

function Find-OptionalFeature() {
[CmdletBinding()]
[OutputType([Bool])]
param (
[Parameter(Mandatory = $true)]
[String] $OptionalFeature
)

If (Get-WindowsOptionalFeature -Online -FeatureName $OptionalFeature) {
return $true
}
Else {
Write-Status -Symbol "?" -Type $TweakType -Status "The $OptionalFeature optional feature was not found." -Warning
return $false
}
}

function Set-OptionalFeatureState() {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[Switch] $Disabled,
[Parameter(Mandatory = $false)]
[Switch] $Enabled,
[Parameter(Mandatory = $true)]
[Array] $OptionalFeatures,
[Parameter(Mandatory = $false)]
[Array] $Filter,
[Parameter(Mandatory = $false)]
[ScriptBlock] $CustomMessage
)

$Script:SecurityFilterOnEnable = @("IIS-*")
$Script:TweakType = "OptionalFeature"

ForEach ($OptionalFeature in $OptionalFeatures) {
If (Find-OptionalFeature $OptionalFeature) {
If (($OptionalFeature -in $SecurityFilterOnEnable) -and ($Enabled)) {
Write-Status -Symbol "?" -Type $TweakType -Status "Skipping $OptionalFeature to avoid a security vulnerability ..." -Warning
Continue
}

If ($OptionalFeature -in $Filter) {
Write-Status -Symbol "?" -Type $TweakType -Status "The $OptionalFeature will be skipped as set on Filter ..." -Warning
Continue
}

If (!$CustomMessage) {
If ($Disabled) {
Write-Status -Symbol "-" -Type $TweakType -Status "Uninstalling the $OptionalFeature optional feature ..."
}
ElseIf ($Enabled) {
Write-Status -Symbol "+" -Type $TweakType -Status "Installing the $OptionalFeature optional feature ..."
}
Else {
Write-Status -Symbol "?" -Type $TweakType -Status "No parameter received (valid params: -Disabled or -Enabled)" -Warning
}
}
Else {
Write-Status -Symbol "@" -Type $TweakType -Status $(Invoke-Expression "$CustomMessage")
}

If ($Disabled) {
Get-WindowsOptionalFeature -Online -FeatureName $OptionalFeature | Where-Object State -Like "Enabled" | Disable-WindowsOptionalFeature -Online -NoRestart
}
ElseIf ($Enabled) {
Get-WindowsOptionalFeature -Online -FeatureName $OptionalFeature | Where-Object State -Like "Disabled*" | Enable-WindowsOptionalFeature -Online -NoRestart
}
}
}
}

<#
Set-OptionalFeatureState -Disabled -OptionalFeatures @("OptionalFeature1", "OptionalFeature2", "OptionalFeature3")
Set-OptionalFeatureState -Disabled -OptionalFeatures @("OptionalFeature1", "OptionalFeature2", "OptionalFeature3") -Filter @("OptionalFeature3")
Set-OptionalFeatureState -Disabled -OptionalFeatures @("OptionalFeature1", "OptionalFeature2", "OptionalFeature3") -Filter @("OptionalFeature3") -CustomMessage { "Uninstalling $OptionalFeature feature!"}
Set-OptionalFeatureState -Enabled -OptionalFeatures @("OptionalFeature1", "OptionalFeature2", "OptionalFeature3")
Set-OptionalFeatureState -Enabled -OptionalFeatures @("OptionalFeature1", "OptionalFeature2", "OptionalFeature3") -Filter @("OptionalFeature3")
Set-OptionalFeatureState -Enabled -OptionalFeatures @("OptionalFeature1", "OptionalFeature2", "OptionalFeature3") -Filter @("OptionalFeature3") -CustomMessage { "Installing $OptionalFeature feature!"}
#>
69 changes: 17 additions & 52 deletions src/scripts/optimize-windows-features.ps1
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
Import-Module -DisableNameChecking $PSScriptRoot\..\lib\"set-windows-feature-state.psm1"
Import-Module -DisableNameChecking $PSScriptRoot\..\lib\"title-templates.psm1"

# Adapted from: https://github.com/ChrisTitusTech/win10script/pull/131/files

function Optimize-WindowsFeaturesList() {
[CmdletBinding()]
param (
[Switch] $Revert,
[Array] $EnableStatus = @(
@{
Symbol = "-"; Status = "Uninstalling";
Command = { Get-WindowsOptionalFeature -Online -FeatureName $Feature | Where-Object State -Like "Enabled" | Disable-WindowsOptionalFeature -Online -NoRestart }
}
@{
Symbol = "+"; Status = "Installing";
Command = { Get-WindowsOptionalFeature -Online -FeatureName $Feature | Where-Object State -Like "Disabled*" | Enable-WindowsOptionalFeature -Online -NoRestart }
}
)
[Switch] $Revert
)
$TweakType = "Feature"

If (($Revert)) {
Write-Status -Symbol "<" -Type $TweakType -Status "Reverting: $Revert." -Warning
$EnableStatus = @(
@{
Symbol = "<"; Status = "Re-Installing";
Command = { Get-WindowsOptionalFeature -Online -FeatureName $Feature | Where-Object State -Like "Disabled*" | Enable-WindowsOptionalFeature -Online -NoRestart }
}
@{
Symbol = "<"; Status = "Re-Uninstalling";
Command = { Get-WindowsOptionalFeature -Online -FeatureName $Feature | Where-Object State -Like "Enabled" | Disable-WindowsOptionalFeature -Online -NoRestart }
}
)
}

Write-Title -Text "Uninstall features from Windows"

$DisableFeatures = @(
"FaxServicesClientPackage" # Windows Fax and Scan
"IIS-*" # Internet Information Services
"Internet-Explorer-Optional-*" # Internet Explorer
"LegacyComponents" # Legacy Components
"MediaPlayback" # Media Features (Windows Media Player)
"MicrosoftWindowsPowerShellV2" # PowerShell 2.0
Expand All @@ -46,37 +21,27 @@ function Optimize-WindowsFeaturesList() {
"Printing-XPSServices-Features" # Microsoft XPS Document Writer
"WorkFolders-Client" # Work Folders Client
)
ForEach ($Feature in $DisableFeatures) {
If (Get-WindowsOptionalFeature -Online -FeatureName $Feature) {
If (($Revert -eq $true) -and ($Feature -like "IIS-*")) {
Write-Status -Symbol "?" -Type $TweakType -Status "Skipping $Feature to avoid a security vulnerability ..." -Warning
Continue
}
Write-Status -Symbol $EnableStatus[0].Symbol -Type $TweakType -Status "$($EnableStatus[0].Status) $Feature ..."
Invoke-Expression "$($EnableStatus[0].Command)"
}
Else {
Write-Status -Symbol "?" -Type $TweakType -Status "$Feature was not found." -Warning
}
}

Write-Title -Text "Install features for Windows"

$EnableFeatures = @(
"NetFx3" # NET Framework 3.5
"NetFx4-AdvSrvs" # NET Framework 4
"NetFx4Extended-ASPNET45" # NET Framework 4.x + ASPNET 4.x
)

ForEach ($Feature in $EnableFeatures) {
If (Get-WindowsOptionalFeature -Online -FeatureName $Feature) {
Write-Status -Symbol "+" -Type $TweakType -Status "Installing $Feature ..."
Get-WindowsOptionalFeature -Online -FeatureName $Feature | Where-Object State -Like "Disabled*" | Enable-WindowsOptionalFeature -Online -NoRestart
}
Else {
Write-Status -Symbol "?" -Type $TweakType -Status "$Feature was not found." -Warning
}
Write-Title -Text "Optional Features Tweaks"
Write-Section -Text "Uninstall Optional Features from Windows"

If ($Revert) {
Write-Status -Symbol "<" -Type "OptionalFeature" -Status "Reverting the tweaks is set to '$Revert'." -Warning
$CustomMessage = { "Re-Installing the $OptionalFeature optional feature ..." }
Set-OptionalFeatureState -Enabled -OptionalFeatures $DisableFeatures -CustomMessage $CustomMessage
}
Else {
Set-OptionalFeatureState -Disabled -OptionalFeatures $DisableFeatures
}

Write-Section -Text "Install Optional Features from Windows"
Set-OptionalFeatureState -Enabled -OptionalFeatures $EnableFeatures
}

function Main() {
Expand All @@ -89,7 +54,7 @@ function Main() {
# List all Windows Capabilities:
#Get-WindowsCapability -Online | Select-Object -Property State, Name | Sort-Object State, Name | Out-GridView

If (!($Revert)) {
If (!$Revert) {
Optimize-WindowsFeaturesList # Disable useless features and Enable features claimed as Optional on Windows, but actually, they are useful
}
Else {
Expand Down

0 comments on commit f8ae489

Please sign in to comment.