Kasper Hansen
Kasper Hansen

Reputation: 6557

How to enable a Windows feature via Powershell

I need to enable two Windows Features using Powershell. But I don't know their names or how to find them.

Windows Features

So far I have managed to install the IIS and stop the Default Application Pool using a script found here.

function InstallFeature($name) {
    cmd /c "ocsetup $name /passive"
}
InstallFeature IIS-WebServerRole
    InstallFeature IIS-WebServer
        InstallFeature IIS-CommonHttpFeatures
            InstallFeature IIS-DefaultDocument
            InstallFeature IIS-DirectoryBrowsing
            InstallFeature IIS-HttpErrors
            InstallFeature IIS-HttpRedirect
            InstallFeature IIS-StaticContent
        InstallFeature IIS-HealthAndDiagnostics
            InstallFeature IIS-CustomLogging
            InstallFeature IIS-HttpLogging
            InstallFeature IIS-HttpTracing
            InstallFeature IIS-LoggingLibraries
        InstallFeature IIS-Security
            InstallFeature IIS-RequestFiltering
            InstallFeature IIS-WindowsAuthentication
        InstallFeature IIS-ApplicationDevelopment
            InstallFeature IIS-NetFxExtensibility
            InstallFeature IIS-ISAPIExtensions
            InstallFeature IIS-ISAPIFilter
            InstallFeature IIS-ASPNET
    InstallFeature IIS-WebServerManagementTools 
        InstallFeature IIS-ManagementConsole 
        InstallFeature IIS-ManagementScriptingTools

import-module WebAdministration

Stop-WebAppPool DefaultAppPool

Solution

To search:

Get-WindowsFeature *ASP*
Get-WindowsFeature *activation*

To install:

Add-WindowsFeature NET-Framework-45-ASPNET
Add-WindowsFeature NET-HTTP-Activation

Upvotes: 30

Views: 86459

Answers (4)

Ovidiu Bestea
Ovidiu Bestea

Reputation: 1

The easiest way (it worked for me) is to:

  • take a computer where the necessary roles and features are not installed
  • go to Add Server Roles and Features (in Server Manager)
  • use the wizard to add the necessary roles and features
  • on the last screen, before pressing the "Install" button, you will notice the link "Export configuration settings"

Export configuration settings

Save the XML file.

After you have the XML file, you can use the PowerShell script listed below to invoke the "Install-WindowsFeature" cmdlet (Server 2012, 2016).

NOTE1: the PowerShell script was designed to work on Windows Server 2008, 2012 and 2016. For Windows Server 2008 the cmdlet is Add-WindowsFeature.

NOTE2: The PowerShell script assumes the XML file is in the same folder and it's name is listed inside the script - please see the lines 3 and 4.

Import-Module ServerManager -ErrorAction Stop

$win2k8xml = '.\features-w2k8.xml'
$win2k12xml = '.\RolesAndFeatures.xml'


$minOSVersion = [version] "6.1.7600" # Windows Server 2008 R2 RTM version
$os = Get-WmiObject Win32_OperatingSystem
$currentVersion = [version]$os.Version

if($currentVersion -lt $minOSVersion)
{
    throw "OS version equal or greater than ${minOSVersion} is required to run this script"
}
elseif($currentVersion.ToString().substring(0,3) -eq $minOSVersion.ToString().substring(0,3))
{
If (!(Test-Path $win2k8xml)) {Write-Host "For Windows Server 2008 R2 server make sure that you have Features-W2K8.xml in the current folder" -ForegroundColor Yellow; Pause}
}
elseif($currentVersion -gt $minOSVersion){                                                            

If (!(Test-Path $win2k12xml)) {Write-Host "For Windows Server 2012/2016 make sure that you have RolesAndFeatures.xml in the current folder" -ForegroundColor Yellow; Pause}
}

$OSVersionName = (get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName
Write-Host "Your OS version is:$OSVersionName" -ForegroundColor Green

$defaultComputerName = $env:computername
$ComputerName = Read-Host "Computername (Press Enter for current computer - $defaultComputerName)"


if ([string]::IsNullOrEmpty($ComputerName))
{
    $ComputerName = $defaultComputerName;
}

Write-host "Installation will take place on the following computers: $ComputerName"



function Invoke-WindowsFeatureBatchDeployment {
    param (
        [parameter(mandatory)]
        [string] $ComputerName,
        [parameter(mandatory)]
        [string] $ConfigurationFilePath
    )

    # Deploy the features on multiple computers simultaneously.
    $jobs = @()
    if(Test-Connection -ComputerName $ComputerName -Quiet){
        Write-Host "Connection succeeded to: " $ComputerName

        if ($currentVersion.ToString().substring(0,3) -eq $minOSVersion.ToString().substring(0,3)) {
        $jobs += Start-Job -Command {
        #Add-WindowsFeature -ConfigurationFilePath $using:ConfigurationFilePath -ComputerName $using:ComputerName -Restart
        $import = Import-Clixml $using:ConfigurationFilePath
        $import | Add-WindowsFeature
        } 
        } 
        elseif ($currentVersion -gt $minOSVersion) {
        $jobs += Start-Job -Command {
        Install-WindowsFeature -ConfigurationFilePath $using:ConfigurationFilePath -ComputerName $using:ComputerName -Restart
        } 
        }    

    }
    else{
        Write-Host "Configuration failed for: "+ $ComputerName + "! Check computer name and execute again"
    }


    Receive-Job -Job $jobs -Wait | Select-Object Success, RestartNeeded, ExitCode, FeatureResult
}
if ($currentVersion.ToString().substring(0,3) -eq $minOSVersion.ToString().substring(0,3)) {$FilePath = Resolve-Path $win2k8xml}
elseif ($currentVersion -gt $minOSVersion) {$FilePath = Resolve-Path $win2k12xml}

Invoke-WindowsFeatureBatchDeployment $ComputerName $FilePath

Upvotes: 0

Loïc MICHEL
Loïc MICHEL

Reputation: 26180

if you are in windows 2008R2 there is a module for this :

Import-Module servermanager

this module exports 3 cmdlets : Get-WindowsFeature, Add-WindowsFeature and remove-WindowsFeature

so you can make someting like get-windowsfeature *frame* to list the .net features and install it via command like Add-WindowsFeature Net-Framework

Upvotes: 22

Greg Bray
Greg Bray

Reputation: 15767

For newer Windows client OS (Windows 10/8.1/8) you cannot use Install-WindowsFeature as that is only for managing features on servers. Trying to use it will cause an error message:

Get-WindowsFeature : The target of the specified cmdlet cannot be a Windows client-based operating system.

There is a DISM Powershell module that you can use to find and install optional features:

gcm -module DISM #List available commands
Get-WindowsOptionalFeature -online | ft #List all features and status
Enable-WindowsOptionalFeature -online -FeatureName NetFx3 -Source e:\Sources\sxs

In the last command -Source e:\Sources\sxs is only required if the feature needs to reference the installation media for source files (usually to fix Error: 0x800f081f The source files could not be found). The .NET Framework version 3.5 seems to be the only one that requires that for the client OS, but there are many others on the Server OS that require referencing the installation media for sources.

Upvotes: 43

Jenska
Jenska

Reputation: 71

Try this to get the names (short) and display names (long descriptions):
get-windowsfeature | format-table -property name,displayname -AutoSize

use this to install them:
Install-WindowsFeature -Name $Name

where $Name is the name property from the get

PS: Install-WindowsFeature has replaced Add-WindowsFeature

Upvotes: 7

Related Questions