Created
November 9, 2025 23:43
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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