Powershell - How to use Get-WindowsOptionalFeature command to "Turn Windows Features On and Off"
Asked Answered
D

2

7

In Windows 10, you can "Turn windows features on and off" in the control panel; you see a screen as such: enter image description here

Let's say I want to select IIS 6 WMI Compatibility by using the Enable-WindowsOptionalFeature command in powershell.

If I run :

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.

Dragonet answered 10/8, 2018 at 15:37 Comment(0)
D
7

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:\WINDOWS\system32
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
Dereism answered 11/8, 2018 at 4:55 Comment(3)
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.Dragonet
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 *Dragonet
Glad it's a better solution for you. As for the online thing, it's a mandatory / required parameters learn.microsoft.com/en-us/powershell/module/dism/… thing unlike the WinSvr cmdlet for this same sort of thing.Dereism
D
5

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...

You can verify that it was enabled by running this:

(Get-WindowsOptionalFeature -Online -FeatureName 'IIS-WMICompatibility').State

Dragonet answered 10/8, 2018 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.