Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 623
Kellen Stuart Avatar asked Aug 10 '18 15:08

Kellen Stuart


People also ask

How do I get Windows features in PowerShell?

To get the windows features and roles available or installed using PowerShell, you need to use the Get-WIndowsFeature cmdlet. That is obvious that Windows features and roles are available only on the server operating systems not on the client operating system.


2 Answers

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
like image 63
postanote Avatar answered Nov 15 '22 02:11

postanote


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

like image 36
Kellen Stuart Avatar answered Nov 15 '22 03:11

Kellen Stuart