title: Powershell check windows optional features disabled
body: <p>I'm trying to check via powershell if some windows 10 optional features are disabled and add all components if any are missing. The $required_IIS_results foreach loop may not be the most efficient method. Here's what I have so far:</p>
  
  <pre><code>$required_IIS_features = @(
      # Windows 10 IIS Features
      "IIS-WebServerRole",
      "IIS-WebServer",
      "IIS-WebServerManagementTools",
      "IIS-ManagementConsole",
      "IIS-CommonHttpFeatures",
      "IIS-CGI",
      "IIS-HttpRedirect",
      "IIS-IPSecurity"
      )
  
  Function Confirm-IISPrerequisites {
      #Check to see if IIS components are installed
      Write-Host "## Determining if all necessary IIS components have been installed" -ForegroundColor Green
      $required_IIS_results = ForEach ($feature in $required_IIS_features) {Get-WindowsOptionalFeature -Online -FeatureName $feature | Where-Object {$_.State -eq "Disabled"}}
  
      If($required_IIS_results){
          Write-Host "Installing Required IIS and CGI module" -ForegroundColor Yellow
          Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole
          Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServer
          Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerManagementTools
          Enable-WindowsOptionalFeature -Online -FeatureName IIS-ManagementConsole
          Enable-WindowsOptionalFeature -Online -FeatureName IIS-CommonHttpFeatures
          Enable-WindowsOptionalFeature -Online -FeatureName IIS-CGI
          Enable-WindowsOptionalFeature -Online -FeatureName IIS-HttpRedirect
          Enable-WindowsOptionalFeature -Online -FeatureName IIS-IPSecurity
      } Else {
          Write-Host "## All of the Necessary IIS Role Services have been installed" -ForegroundColor Green
      }
  }
  #execute function
  Confirm-IISPrerequisites
  </code></pre>