Powershell - How to use Get-WindowsOptionalFeature command to “Turn Windows Features On and Off”
Clash Royale CLAN TAG#URR8PPP
Powershell - How to use Get-WindowsOptionalFeature command to “Turn Windows Features On and Off”
In Windows 10, you can "Turn windows features on and off" in the control panel; you see a screen as such:
Let's say I want to select IIS 6 WMI Compatibility by using the Enable-WindowsOptionalFeature
command in powershell.
Enable-WindowsOptionalFeature
If I run :
Get-WindowsOptionalFeature "IIS 6 WMI Compatibility"
Get-WindowsOptionalFeature "IIS 6 WMI Compatibility"
I get this error:
Get-WindowsOptionalFeature : A positional parameter cannot be found that accepts argument 'IIS 6 WMI Compatibility'.
At line:1 char:1
+ Get-WindowsOptionalFeature "IIS 6 WMI Compatibility"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WindowsOptionalFeature], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Dism.Commands.GetWindowsOptionalFeatureCommand
Question
How do I map the names of these features to the PowerShell command?
End Goal
The end goal is to automate setting up a new developer and his machine.
2 Answers
2
Good you found a answer that works for you, but...
Yet, you don't need a function to use wildcards. Just do this...
Get-WmiObject -Class $Win32_OperatingSystem
SystemDirectory : C:WINDOWSsystem32
Organization :
BuildNumber : 17134
RegisteredUser :
SerialNumber : 00330-50027-66869-AAOEM
Version : 10.0.17134
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.17134.165
PSEdition Desktop
PSCompatibleVersions 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.17134.165
BuildVersion 10.0.17134.165
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
# List features all
(Get-WindowsOptionalFeature -Online -FeatureName '*') | Format-Table -Autosize
(Get-WindowsOptionalFeature -Online -FeatureName '*').Count
144
# List features for IIS
(Get-WindowsOptionalFeature -Online -FeatureName '*IIS*').Count
54
# List features for wmi
(Get-WindowsOptionalFeature -Online -FeatureName '*wmi*').Count
2
# List features for IIS or wmi
(Get-WindowsOptionalFeature -Online -FeatureName '*iis*|*wmi*').Count
55
# List features for IIS or wmi or hyperv
(Get-WindowsOptionalFeature -Online -FeatureName '*iis*|*wmi*|*hyper*').Count
63
for
I think what got me when trying to first learn this command was the
-Online
flag. I don't understand why you get an error if you do Get-WindowsOptionalFeature -FeatureName *
– Kolob Canyon
Aug 13 at 15:31
-Online
Get-WindowsOptionalFeature -FeatureName *
Glad it's a better solution for you. As for the online thing, it's a mandatory / required parameters docs.microsoft.com/en-us/powershell/module/dism/… thing unlike the WinSvr cmdlet for this same sort of thing.
– postanote
Aug 13 at 20:31
I figured it out.
The code below is used to find the feature by using wildcards
$features = Get-WindowsOptionalFeature -Online
Write-Host ('There are ' + $features.Count + ' Windows features available') -ForegroundColor Green
foreach($feature in $features)
if($feature.FeatureName -like "*IIS*WMI*") # wildcard search
$feature
The code above returns this:
There are 170 Windows features available
FeatureName : IIS-WMICompatibility
State : Disabled
Therefore, to enable the feature you can run:
$feature = Get-WindowsOptionalFeature -Online -FeatureName 'IIS-WMICompatibility'
Enable-WindowsOptionalFeature $feature -Online
Note: you have to run Enable-WindowsOptionalFeature
as admin...
Enable-WindowsOptionalFeature
You can verify that it was enabled by running this:
(Get-WindowsOptionalFeature -Online -FeatureName 'IIS-WMICompatibility').State
(Get-WindowsOptionalFeature -Online -FeatureName 'IIS-WMICompatibility').State
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Cool. So I can get rid of my
for
loop and just do that instead. I'll mark as accepted because that's more succinct than my answer.– Kolob Canyon
Aug 13 at 15:26