Skip to content

Instantly share code, notes, and snippets.

@HauptJ
Created November 9, 2025 23:43
Show Gist options
  • Select an option

  • Save HauptJ/d25cb826c4b12c12cd7628a063fdc0ee to your computer and use it in GitHub Desktop.

Select an option

Save HauptJ/d25cb826c4b12c12cd7628a063fdc0ee to your computer and use it in GitHub Desktop.
PowerShell Script to Enable Windows Optional Features - Specifically HyperV and WSL By Default
#Requires -RunAsAdministrator
param (
[string[]] $optFeatures = @("Microsoft-Hyper-V-All", "VirtualMachinePlatform", "Microsoft-Windows-Subsystem-Linux"),
[bool] $optRestart = $False
)
try {
foreach($feature in $optFeatures) {
Enable-WindowsOptionalFeature -Online -FeatureName $feature -All -NoRestart
}
if($optRestart){
Restart-Computer
}
} catch {
# $Error Automatic Variable: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.4#error
Write-Output "An error occurred"
if($Error.Count -gt 0){
Write-Output "Errors thrown: " $Error.Count
foreach($err in $Error){
Write-Output "Error: $err"
}
}
Exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment