Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs
Contact   •   Articles   •   Products   •   Support   •   Search
Ad-free experience sponsored by:
ASPOSE - the market leader of .NET and Java APIs for file formats – natively work with DOCX, XLSX, PPT, PDF, images and more

Automating IIS Feature Installation with Powershell


:P
On this page:

Automagicking

Here's an oldie but goodie, that keeps coming up for me rather frequently. I've been working with IIS on Windows for a loooong time and I have a number of products that go way back that run on IIS. As a result I deal with a lot of support issues around IIS and people who install IIS run an application for years, have their servers eventually break down and then have to reinstall years after their last install. And a lot of times the people who set up the system are long gone.

The chief complaints I hear frequently is that it's a pain to get IIS to install initially with all the right components. I tend to agree - especially on Server versions installing IIS through the insanely user hostile Server Manager interface is a pain.

But there's an easier, quicker and repeatable way if you're willing to dive into the command line or create and run a small Powershell script.

Enter Enable-WindowsOptionalFeature

Apparently many people are unaware that in recent versions of Windows - using Powershell - you can automate the IIS Features installation using a few simple Powershell Commandlet calls. It's as easy as creating a small PowerShell script file and letting her rip.

You can use the Enable-WindowsOptionalFeature command to install IIS Features as well as any other Windows Features. This command works both on desktop and server versions (server versions also have Enable-WindowsFeature which has the same effect) and makes it pretty easy to automate an IIS install by whittling away a few commands in a Powershell script file.

Here's what I typically use to configure my base version of IIS:

# This script installs IIS and the features required to
# run West Wind Web Connection.
#
# * Make sure you run this script from a Powershel Admin Prompt!
# * Make sure Powershell Execution Policy is bypassed to run these scripts:
# * YOU MAY HAVE TO RUN THIS COMMAND PRIOR TO RUNNING THIS SCRIPT!
Set-ExecutionPolicy Bypass -Scope Process

# To list all Windows Features: dism /online /Get-Features
# Get-WindowsOptionalFeature -Online 
# LIST All IIS FEATURES: 
# Get-WindowsOptionalFeature -Online | where FeatureName -like 'IIS-*'

Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole
Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServer
Enable-WindowsOptionalFeature -Online -FeatureName IIS-CommonHttpFeatures
Enable-WindowsOptionalFeature -Online -FeatureName IIS-HttpErrors
Enable-WindowsOptionalFeature -Online -FeatureName IIS-HttpRedirect
Enable-WindowsOptionalFeature -Online -FeatureName IIS-ApplicationDevelopment

Enable-WindowsOptionalFeature -online -FeatureName NetFx4Extended-ASPNET45
Enable-WindowsOptionalFeature -Online -FeatureName IIS-NetFxExtensibility45

Enable-WindowsOptionalFeature -Online -FeatureName IIS-HealthAndDiagnostics
Enable-WindowsOptionalFeature -Online -FeatureName IIS-HttpLogging
Enable-WindowsOptionalFeature -Online -FeatureName IIS-LoggingLibraries
Enable-WindowsOptionalFeature -Online -FeatureName IIS-RequestMonitor
Enable-WindowsOptionalFeature -Online -FeatureName IIS-HttpTracing
Enable-WindowsOptionalFeature -Online -FeatureName IIS-Security
Enable-WindowsOptionalFeature -Online -FeatureName IIS-RequestFiltering
Enable-WindowsOptionalFeature -Online -FeatureName IIS-Performance
Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerManagementTools
Enable-WindowsOptionalFeature -Online -FeatureName IIS-IIS6ManagementCompatibility
Enable-WindowsOptionalFeature -Online -FeatureName IIS-Metabase
Enable-WindowsOptionalFeature -Online -FeatureName IIS-ManagementConsole
Enable-WindowsOptionalFeature -Online -FeatureName IIS-BasicAuthentication
Enable-WindowsOptionalFeature -Online -FeatureName IIS-WindowsAuthentication
Enable-WindowsOptionalFeature -Online -FeatureName IIS-StaticContent
Enable-WindowsOptionalFeature -Online -FeatureName IIS-DefaultDocument
Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebSockets
Enable-WindowsOptionalFeature -Online -FeatureName IIS-ApplicationInit
Enable-WindowsOptionalFeature -Online -FeatureName IIS-ISAPIExtensions
Enable-WindowsOptionalFeature -Online -FeatureName IIS-ISAPIFilter
Enable-WindowsOptionalFeature -Online -FeatureName IIS-HttpCompressionStatic

Enable-WindowsOptionalFeature -Online -FeatureName IIS-ASPNET45

# If you need classic ASP (not recommended)
#Enable-WindowsOptionalFeature -Online -FeatureName IIS-ASP


# The following optional components require 
# Chocolatey OR Web Platform Installer to install


# Install UrlRewrite Module for Extensionless Urls (optional)
###  & "C:\Program Files\Microsoft\Web Platform Installer\WebpiCmd-x64.exe" /install /Products:UrlRewrite2 /AcceptEULA /SuppressPostFinish
#choco install urlrewrite -y
    
# Install WebDeploy for Deploying to IIS (optional)
### & "C:\Program Files\Microsoft\Web Platform Installer\WebpiCmd-x64.exe" /install /Products:WDeployNoSMO /AcceptEULA /SuppressPostFinish
# choco install webdeploy -y

# Disable Loopback Check on a Server - to get around no local Logins on Windows Server
# New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name "DisableLoopbackCheck" -Value "1" -PropertyType dword

Put the above into a Powershell script file (SetupIIS.ps1) and then:

  • Open a PowerShell command prompt using Run as Administrator
  • Run the script

You can tweak and fiddle with the features you actually need for IIS, but the above is pretty standard for my base installs.

To see all the features available related to IIS you can use:

Get-WindowsOptionalFeature -Online | where FeatureName -like 'IIS-*'

Additional IIS Features

Two more features that I typically use on IIS and aren't directly includable features are WebDeploy and UrlRewrite. You can install those from the Web Platform installer, or - which is easier in my case - from Chocolatey:

choco install webdeploy -y
choco install urlrewrite -y

What Windows Optional Features are Available?

Enable-WindowsOptionalFeature is great, as long as you know what's available. Luckily it's easy to figure out what's available and what's installed and what's not.

Check for Installed Features:

Get-WindowsOptionalFeature -Online | where {$_.state -eq "Enabled"} | ft -Property featurename

Check for Features available but Not Installed

Get-WindowsOptionalFeature -Online | where {$_.state -eq "Disabled"} | ft -Property featurename

Disable a Windows Feature

Disable-WindowsOptionalFeature -Online -FeatureName IIS-DirectoryBrowsing

Create an IIS Application Pool and Web Site

For bonus points, lets also create an Application Pools and attach a Web site/Virtual to it. You can configure IIS via more powershell helpers by using the WebAdministration powershell module (most like already installed):

Import-Module WebAdministration 

Once installed it's easy to create a new WebSite and Application Pool.

To create an Application Pool:

New-WebAppPool -name "NewWebSiteAppPool"  -force

$appPool = Get-Item -name "NewWebSiteAppPool" 
$appPool.processModel.identityType = "NetworkService"
$appPool.enable32BitAppOnWin64 = 1
$appPool | Set-Item

Then to create a Web Site:

md "c:\Web Sites\NewWebSite"

# All on one line
$site = $site = new-WebSite -name "NewWebSite" 
                            -PhysicalPath "c:\Web Sites\NewWebSite" 
                            -HostHeader "home2.west-wind.com"
                            -ApplicationPool "NewWebSiteAppPool" 
                            -force

Discover Features with PowerShell ISE

There are obviously a lot more options you can set on these components, but it's easy to find out about those. I also recommend that while you're discovering features, use the PowerShell ISE shell (run from the Start menu using Run as Administrator) to discover what's available:

Figure 1 - Powershell ISE lets you get Intellisense on commands and live object instances

The Intellisense in the editor and the command window gives you live property values on commands and even live objects as shown in the Figure 1 which makes it relatively easy to figure out settings. For the rest the various cmd-lets and admin objects are well documented and searchable.

Summary

None of this is new of course, but it's always good to be reminded that you can automate installation and configuration of IIS relatively easily. This is especially true since I just this week I heard from several people how much of a pain IIS can be to install and get up and running. It doesn't have to be this way... the tools are there.

this post created and published with Markdown Monster
Posted in IIS  Windows  

The Voices of Reason


 

Rich
May 26, 2017

# re: Automating IIS Feature Installation with Powershell

Very useful Rick! For some reason IIS does not come as standard with the very useful feature to import an application as a ZIP file and Microsoft make it super hard to find the link to download that add-on. It forces you to first install the "Web Platform Platform Installer Platform" or something, and then find an obscure link. I'm guessing that can't be done from PowerShell, but it sure would be useful.


SomeUser
May 27, 2017

# re: Automating IIS Feature Installation with Powershell

Rich,

Web Deploy can be downloaded without WebPI (https://www.iis.net/downloads/microsoft/category/deploy-and-migrate). While it is possible to use PowerShell (or any other scripting toy, or msiexec) to get the bits installed, the installer's user interface is the only documented way to fully customize the setup.


Jon
May 30, 2017

# re: Automating IIS Feature Installation with Powershell

This looks good, Rick. I ended up doing something similar with Desired State Configuration (DSC) to configure IIS the way that I wanted. https://msdn.microsoft.com/en-us/powershell/dsc/windowsfeatureresource This simplifies the re-running of my configuration script, but doesn't always provide the level of granularity that I need. I combined it with non-DSC PowerShell that manually checks to see if things are already configured, and now have a re-runnable script that I can run on my "pet" servers. Maybe this will become less important once I start treating my servers more like cattle, and stand up new ones every deployment.


Jan
August 20, 2017

# re: Automating IIS Feature Installation with Powershell

Does anyone know, how to enable (IIS 6 Management Console, IIS 6 Scripting Tools, IIS 6 WMI Compatibility) using command line? This is for Windows 7 Enterprise 64 bit


Chad
October 18, 2017

# re: Automating IIS Feature Installation with Powershell

I second the need for a powershell script for IIS 6. I'm currently writing an automated installation for BizTalk 2016 and part of the prerequisites are:

For Windows 10 In Web Management Tools: also check: IIS 6 Management Compatbility IIS 6 Management Console IIS 6 Scripting Tools IIS Metabase and IIS confirguration compatibility IIS Management Console

In Wold Wide Web Services, expand Security and also check: Basic Authentication Windows Authentication


Rick Strahl
October 18, 2017

# re: Automating IIS Feature Installation with Powershell

@Jan - use Get-WindowsOptionalFeature and look through the list - it's in there. See how I query for features not installed int the post.


John
March 18, 2018

# re: Automating IIS Feature Installation with Powershell

Thanks for posting Rick, very useful.

I got an error on Windows 2016 (1607):- IIS-NetFxExtensibility, IIS-NetFxExtensibility45, IIS-ASPNET45 all errored with:

Enable-WindowsOptionalFeature : One or several parent features are disabled so current feature can not be enabled.

I enabled the following feature and then the above worked: NetFx4Extended-ASPNET45

Enable-WindowsOptionalFeature -Online -FeatureName NetFx4Extended-ASPNET45

Mikey
June 07, 2018

# re: Automating IIS Feature Installation with Powershell

Nice! -- I noticed a couple of these error out because a parent feature is not installed; it looks like if you add the "-All" switch to end of each of the "Enable-WindowsOptionalFeature" commands, then you will not encounter this issue (also, some of the commands are listed more than once, I'm guessing that was to combat this issue, but with -All, it's not necessary anymore).

Also, I prepended my script with this (to make sure it only ran if IIS was missing):

$installed = (Get-WindowsOptionalFeature -Online | `
    where { $_.state -eq "Enabled" -and $_.FeatureName -eq "IIS-WebServer" } | Measure-Object).Count;

if ($installed -gt 0) { return; }

Alberto
June 14, 2018

# re: Automating IIS Feature Installation with Powershell

Great article! I've found two small problems on Windows 10 1709:

  1. add Enable-WindowsOptionalFeature -Online -FeatureName NetFx4Extended-ASPNET45 instead of Enable-WindowsOptionalFeature -Online -FeatureName IIS-NetFxExtensibility45 (which probably is a typing mistake)

  2. you have to put the Enable-WindowsOptionalFeature -Online -FeatureName IIS-ASPNET45 as the last line of the script: instead you'll get a registry error (which probably is the "One or several parent features are disabled so current feature can not be enabled." on Winsrv 2016 ) Thanks!


Anupam
February 16, 2019

# re: Automating IIS Feature Installation with Powershell

Super article Rick, Thanks ! is there a way to figure out what minimal set of components are required, by looking at the web.config of a webapp?


Rick Strahl
February 17, 2019

# re: Automating IIS Feature Installation with Powershell

@anupam - Nope - not really. You know you'll need ASP.NET which means you'll need most things I mentioned above but it's still on a case by case basis to pick out what you need.


JoeM
March 01, 2019

# re: Automating IIS Feature Installation with Powershell

A couple of failings here: You never once mention how/where to get "WebPlatform Installer;" nor "chocolatey" I love chocolate; but you seem to have built-in assumptions that all readers magically know what there are and where to find them. One commenter at least provided a link to WebDeploy.

Those are essential refernces, if you expect readers easily be able to easily follow this. That said, this is AMAZING work - nothing short of 'really cool!'. I've used this quite a bit since you first posted it. THANK YOU!

So, what is this magical "WebPlatform Installer" - is it an MS piece? Another feature? Where can I get it? And, same question for "chocolatey" - I will Google for these vs. waiting for a response, but ???


Rick Strahl
March 02, 2019

# re: Automating IIS Feature Installation with Powershell

@Josh - you are right in that I make some basic assumptions here. This is a developer Weblog and I post developer stuff and if you're not a developer dealing with this stuff this blog will be a slog.

That said - if you can't figure out how to search for Web Platform Installer or Chocolatey then you probably are not in the intended audience. No offense, but true. That doesn't mean you can't use any of this, you just need to do a little extra work to add to your repertoire of tools. If you are administering a Web Server those two distribution tools will be critical and it'll be a worthwhile. Happy searching...


Jayson
April 16, 2019

# re: Automating IIS Feature Installation with Powershell

I love powershell for installing almost everything now. This one works flawlessly for a standard Laserfiche Web deployment. Thanks so much!


Sam
July 11, 2019

# re: Automating IIS Feature Installation with Powershell

Rick,

Thank you so much for all of your posts!

You have helped me tremendously, with your concise explanations and cogent, clear tips!

You have saved me so much time.

Thanks, Sam


John
July 12, 2019

# re: Automating IIS Feature Installation with Powershell

Rick,

I am not sure what happened to my previous post. But, I am trying to have this installed on Windows 10 desktop for developers. Default it is going to be installed on C drive. Now, with the C drive being a VDISK and if it is locked down that users cannot make changes, will it affect in anyway if they have develop the websites and put it in InetPub folder in D drive? Next, I also got a script from this Microsoft link which be used to automate the relocating of the Inetpub contents, and will leave the existing directory structure untouched. Will this affect your installation in anyway? I am going to build a new Windows 10 box with C drive and D drive and I am going to run your script to install IIS and also I am going to run this script to see how it goes.

https://support.microsoft.com/en-us/help/2752331/guidance-for-relocation-of-iis-7-0-and-iis-7-5-content-directories


Michael
August 21, 2019

# re: Automating IIS Feature Installation with Powershell

I found this article useful, but I already had another method of achieving the same result.

However, I wanted to provide what I changed to make the script above 'less redundant'

# This script installs IIS and the features required to
# run West Wind Web Connection.
#
# * Make sure you run this script from a Powershell Admin Prompt!
# * Make sure Powershell Execution Policy is bypassed to run these scripts:
# * YOU MAY HAVE TO RUN THIS COMMAND PRIOR TO RUNNING THIS SCRIPT!

Set-ExecutionPolicy Bypass -Scope Process

# To list all Windows Features: dism /online /Get-Features
# Get-WindowsOptionalFeature -Online 
# LIST All IIS FEATURES: 
# Get-WindowsOptionalFeature -Online | where FeatureName -like 'IIS-*'

$List = @( )

$List += "WebServerRole"    , "WebServer"       , "CommonHTTPFeatures"     , 
         "HTTPErrors"       , "HTTTPRedirect"   , "ApplicationDevelopment" | % { "IIS-$_" }

$List += "NetFX4Extended-ASPNET45"

$List += "NetFXExtensibility45"     , "HealthAndDiagnostics"        , "HTTPLogging"            , 
         "LoggingLibraries"         , "RequestMonitor"              , "HTTPTracing"            ,
         "Security"                 , "RequestFilgering"            , "Performance"            , 
         "WebServerManagementTools" , "IIS6ManagementCompatibility" , "Metabase"               ,
         "ManagementConsole"        , "BasicAuthentication"         , "WindowsAuthentication"  , 
         "StaticContent"            , "DefaultDocument"             , "WebSockets"             ,
         "ApplicationInit"          , "ISAPIExtensions"             , "ISAPIFilter"            , 
         "HTTPCompressionStatic"    , "ASPNET45"                    | % { "IIS-$_" }

Get-WindowsOptionalFeature -Online | Select FeatureName , State | Sort FeatureName | % { 

    ForEach ( $i in $List ) 
    { 
        If ( $i -eq $_.FeatureName -and $_.State -ne "Enabled" ) 
        { 
            Enable-WindowsOptionalFeature -Online -FeatureName $i 
        }
    }
}

# If you need classic ASP (not recommended)
#Enable-WindowsOptionalFeature -Online -FeatureName IIS-ASP


# The following optional components require 
# Chocolatey OR Web Platform Installer to install


# Install UrlRewrite Module for Extensionless Urls (optional)
###  & "C:\Program Files\Microsoft\Web Platform Installer\WebpiCmd-x64.exe" /install /Products:UrlRewrite2 /AcceptEULA /SuppressPostFinish
#choco install urlrewrite -y

# Install WebDeploy for Deploying to IIS (optional)
### & "C:\Program Files\Microsoft\Web Platform Installer\WebpiCmd-x64.exe" /install /Products:WDeployNoSMO /AcceptEULA /SuppressPostFinish
# choco install webdeploy -y

# Disable Loopback Check on a Server - to get around no local Logins on Windows Server
# New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name "DisableLoopbackCheck" -Value "1" -PropertyType dword

At any rate, I didn't change much beyond the main block, and when testing, it does appear to have an issue being able to pull from a management tool or parent feature, so I may get around to fleshing it out a bit more... I just liked what I saw, it's a lot like the version I put together ... (this script does far more than just setting up IIS... which is around the line '1882' mark )

https://github.com/mcc85s/PSD-Remaster/blob/master/Install/Provision-Hybrid.ps1

It's in a section where adding a BITS/IIS server is an option... bears a lot of similarities to what you have here, but, goes in far more depth.

Still, I learn things when I see how other people approach similar tasks. So, kudos for making this... and feel free to use anything you see from my end.

-MC


Rick Strahl
August 21, 2019

# re: Automating IIS Feature Installation with Powershell

@Michael - nice


Pieter Siegers
August 31, 2019

# re: Automating IIS Feature Installation with Powershell

All, My current client is using the WIX Toolset to create a MSI for the installation of their product. Now I have the requirement to check if IIS-WebSockets are installed - if so, enable the installation of a specific website that makes use of it.

So, sofar I found the following options:

  1. use a WIX RegistrySearch to find out if IIS-WebSockets are enable or not;
  2. use a WIX FileSearch to find out if the websocket.dll exists on the target machine;
  3. run a PowerShell script inside WIX (silent, no UI) that determines if the IIS-WebSockets component is installed and if it is enabled or not.

Notes on number 1: until now I have found a couple of Registry keys on the web that either simply do not exist on Windows 10, or do not seem to indicate whether the component is enabled or not, so it's not very useful. To give an example: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Notifications\OptionalFeatures\IIS-WebSockets (DWORD values stay 0 regardles of WebSocket Protocol enabled or disabled).

Notes on number 2: The dll may exists on the target machine or not, but that still does not give me any clue whether it is enabled or not. So also not very useful.

Notes on number 3: This may well give me the information I need, so it sounds like the perfect option, but then the installer gets dependent on whether the PowerShell utility is installed or not.

Question is: Can you or anybody give me any advice to what my best option is in this particular case? Thanks a lot in advance for any help you can offer! Regards, Pieter


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2019