Skip to content

Instantly share code, notes, and snippets.

@jmconway
Last active May 11, 2021 18:28
Show Gist options
  • Save jmconway/27d02c0ba8adb9622a7d6822130e8f61 to your computer and use it in GitHub Desktop.
Save jmconway/27d02c0ba8adb9622a7d6822130e8f61 to your computer and use it in GitHub Desktop.
Windows PowerShell script to set Optional Features on the image deployed with WDS/MDT. Has a few parameters specific to my use cases, but otherwise the $toDisable and $toEnable variables include the FeatureName's of optional features I can universally disable in my deployments/environment.
param(
# Specify "-Legacy" if Internet Explorer is still needed for compatibility with apps
[Parameter(Mandatory=$false)]
[Switch]
$Legacy,
# Specify "-IncludeWSL" to enable Windows Subsystem for Linux for IT Staff or approved faculty
[Parameter(Mandatory=$false)]
[Switch]
$IncludeWSL,
# Specify "-IncludeHyperV" to enable Hyper-V Virtualization for IT Staff or approved users
[Parameter(Mandatory=$false)]
[Switch]
$IncludeHyperV
)
# Features Enabled by Default that we Disable
$toDisable = "Printing-XPSServices-Features","WCF-Services45","WCF-TCP-PortSharing45","WorkFolders-Client","WindowsMediaPlayer","MicrosoftWindowsPowerShellV2","MicrosoftWindowsPowerShellV2Root","FaxServicesClientPackage","Microsoft-Windows-Client-EmbeddedExp-Package"
# If -Legacy switch is NOT used, add Internet Explorer to the array
if (!$Legacy) {
$toDisable += "Internet-Explorer-Optional-amd64"
}
# Features Disabled by Default that we Enable
$toEnable = "Containers","Containers-DisposableClientVM","Windows-Defender-ApplicationGuard"
# If the -IncludeWSL switch IS used, add WSL to the array
if ($IncludeWSL) {
$toEnable += "Microsoft-Windows-Subsystem-Linux"
}
# If the -IncludeHyperV switch IS used, add Hyper-V Features to the array
if ($IncludeHyperV) {
# There are multiple corresponding features so leveraging Get-WindowsOptionalFeature cmdlet to collect FeatureName's for us.
$HyperVFeatures = Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -like 'Microsoft-Hyper-V*'} | Select-Object FeatureName
# Add all Hyper-V features collected to the array
$toEnable += $HyperVFeatures
}
# Disable the Features collected through the logic above
ForEach ($feat in $toDisable) {
# If the feature is indeed enabled, then disable it. Otherwise one less array item to iterate through.
if ($feat.State -eq 'Enabled') {
Disable-WindowsOptionalFeature -Online -FeatureName $feat -NoRestart -Remove
}
}
# Enable the Features collected through the logic above
ForEach ($feat in $toEnable) {
# If the feature is indeed disabled, then enable it. Otherwise one less array item to iterate through.
if ($feat.State -eq 'Disabled') {
Enable-WindowsOptionalFeature -Online -FeatureName $feat -NoRestart -All
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment