If you manage Windows Servers, you’ve likely worked with Internet Information Services (IIS). Websites are one of IIS’s main features and, using PowerShell, you can easily manage and automate IIS with PowerShell IIS scripts!
In this article, you’ll be introduced to a new way to manage IIS using PowerShell. Using the techniques in this article, you can easily build PowerShell IIS scripts to create and manage IIS websites.
To manage IIS with PowerShell requires one of two PowerShell modules – WebAdminstration or IISAdminstration. This article covers the more modern IISAdministration module. The companion video to this article demonstrates the older WebAdministration module if you’d like to see an alternative way.
Knowing how to manage even a simple website for your company or for the world at large will distinguish you from the rest of your peers. The following sections in this article will help you learn how to create and manage a website in IIS. You will also learn how to manage web application pools using a few PowerShell cmdlets.
Table of Contents
IISAdministration vs WebAdministration Modules
You have two options for managing IIS. You can use WebAdministration or IISAdministration. But which one should you use?
If you’re using IIS IIS 10.0 or higher, the newer IISAdministration module offers a lot of improvements over the older WebAdministration module.
Here are a few highlights:
- IISAdministration cmdlets offer better support for the pipeline and scale better.
- Compared to the older, legacy IIS WebAdministration cmdlets, IISAdministration contains more efficient code.
- IISAdministration cmdlets are simpler, and provide direct access to the server manager.
This article is going to focus on this newer IISAdministration module.
This article will be hands-on. If you intend to follow along, be sure you have the following prerequisites in place.
- Minimum of Windows PowerShell 5.1 or PowerShell 6+
- IISAdministration PowerShell Module from the PowerShell Gallery
- IIS 10.0 Windows Feature Enabled
It’s recommended that you be familiar with basic of HTML. There will be a step that HTML use plays a role in the upcoming examples. But don’t worry, you will be able to copy and paste the code!
All examples in this blog post were created using IIS 10.0 on Windows 10 and PowerShell 7 Preview 6. The IISAdministration cmdlets are the same for Windows Server 2016 & 2019 and work with Windows PowerShell 5.1.
That’s it for the tools used, now for the resources needed to follow along.
Installing the IISAdministration Module
Once you have met the prerequisites, it’s time for some setup work. Luckily, there’s not much setup to do since the PowerShell module you need (IISAdministration) is freely available in the PowerShell Gallery.
Go ahead and open up an elevated PowerShell console on your web server and install the module using the command below.
PS> Install-Module -Name 'IISAdministration'
If you do not have internet access, you can download the module to another host and copy the module to your modules directory. Take a look at the documentation for the
Discovering Available Cmdlets
As with any PowerShell module that you may not be familiar with, one of the first cmdlets you should use to discover a modules cmdlet inventory is
Get-Command. Using the command
Get-Command -Module 'IISAdministration' as shown below will allow you to see what’s possible with this module.
Even though you’ll be learning some common actions to take on websites, these commands will give you an idea on everything else you can do.
Now you have a list of available cmdlets. It’s time to begin putting a site together.
Creating Your First Website
To have a website to work with, let’s first create a website. To do that, the first step is to create a directory to save the web content. Additionally, you’ll need an HTML file in that directory for IIS to display. This directory will serve as the web directory that will be used in later examples.
Creating the Web Folder and Page
First, create a directory. To do that, you can use the
New-Item cmdlet. The code below will create a directory at C:\MyWebsite.
PS> New-Item -ItemType Directory -Name 'MyWebsite' -Path 'C:\'
You can choose whatever path you wish with the above code. To keep things simple, generic paths will be used.
Once you have created the directory, it’s time to create an HTML file that will be the landing page for your new website.
Now create a file called index.html in the C:\Mywebsite directory as shown below.
PS> New-Item -ItemType File -Name 'index.html' -Path 'C:\MyWebsite\'
Once the file is created, open it up in your favorite editor and copy and paste the code below into that document. This HTML is a simple page that will be used to ensure IIS is configured properly when you’re done.
<!DOCTYPE html> <html> <head> <title>IIS Administration With PowerShell Demo</title> </head> <body> <h1>IIS Administration with PowerShell Demo</h1> <p>Thank you for reading this post on how to administer IIS with PowerShell!</p> <p>This page was created using the newer IISAdministration PowerShell module.</p> <h2>First Steps</h2> <p>Keep calm and learn PowerShell.</p> </body> </html>
Creating an IIS Website
It’s now time to make the webpage just created available for the world to see. To do that, you’ll need to create a virtual directory. The virtual directory contains all of the assets and resources used for the page being served.
By default, IIS creates a virtual directory under C:\inetpub\wwwroot. The directory contains all of the default webpage assets and resources for IIS. You could use the default C:\inetpub\wwwroot directory to store your files as well, but for these examples, it will not be used to keep the paths as short as possible. That’s why you are using C:\MyWebsite instead.
To create a new virtual directory (site), use the
New-IISSite cmdlet. This cmdlet allows you to create the IIS website.
For this article, create a website called Mywebsite pointed to the C:\MyWebsite directory that’s listening on port 8088. In the code snippet below, you can see how to do this using the
The only piece that might be confusing is the
BindingInformation parameter. This parameter allows only binding syntax. In this snippet,
New-IISSite will create a website that is bound to an IP address (
* is the same as 127.0.0.1), the TCP port to listen on followed by any HTTP headers.
Binding syntax can be explained as protocol, followed by source:Port:destination . In this example, you’ve configured IIS to listen for HTTP requests from ANY () IP address on port 8088 and the destination is the localhost.*
PS> New-IISSite -Name 'MyWebsite' -PhysicalPath 'C:\MyWebsite\' -BindingInformation "*:8088:"
Once the site has been created, you can run
Get-IISSite to inspect what it looks like as shown below.
By default, the website configuration is started and your web host begins serving those HTTP requests. Open your browser of choice and navigate to the local machine pointing to port 8088. You should now see the webpage you created earlier displayed.
Congratulations! You’ve just set up your first website on IIS using PowerShell!
Now that you have a website up and running, it’s time to learn how to manage your website and the IIS server hosting it.
Managing the IIS Website
Once a website is up running, you will undoubtedly need to perform common actions like stopping and starting the site. For example, if the site needs updates made to it, you’ll sometimes have to restart the site.
Stopping a website is similar to stopping a Windows service. You are preventing or allowing access to the site accessible by a protocol being served on a unique port by IIS. However, you are not stopping the IIS host. Sites can be stopped and started on an individual basis.
Stopping a Single Site
To stop an IIS website, you will use the
Stop-IISSite cmdlet. By default, the site that you created earlier should be in a
Started state. If you stop the site and thus prevent access to MyWebsite, run
Stop-IISSite providing the name as shown below. This code stops the MyWebsite site from being served by IIS. After stopping the site, you will no longer be able to access it.
PS> Stop-IISSite -Name "MyWebsite"
An example of inspecting the website’s state as
Started, stopping and inspecting the state again is shown below. In this example, you are:
- Getting the status of the MyWebsite site using
- Execute the
Stop-IISSitecmdlet against the MyWebsite site. This cmdlet shuts down access to the website. Since this action is making operational changes you will be prompted for confirmation.
Get-IISSiteagain to verify that MyWebsite site is stopped by noticing the
Stopping All Sites
Above you stopped a single site but you can also stop all sites just as easily. By using the pipeline and piping the output of
Stop-IISSite, PowerShell will enumerate all sites and stop them all. In the below code example, the optional
Verbose switch parameter is used to show more information.
Get-IISSite | Stop-IISSite -Verbose
You can also stop all sites in IIS by stopping the W3SVC or World Wide Web Publishing Service. Stopping the Windows service shuts down IIS and prevents all sites being served. If you do this, however, the sites may still show to be in a Started state.
Starting a Single Site
Let’s say you’ve made some changes to the MyWebsite site and need to start it back up again. It’s time to use the
Start-IISSite command. To do that, simply specify the name just as you did by using the
PS> Start-IISSite -Name 'MyWebsite'
When executed, you can then inspect the state again using
Get-IISSite. When you do, you should then see similar output to the screenshot below.
And now your site should be available again at http://localhost:8088 if you refresh your browser.
Starting All Sites
Similar to how you stopped all sites from being published, you can use the pipeline to start all websites on your IIS host as well.
PS> Get-IISSite | Start-IISSite
Once again, you are getting all of the IIS sites being served and using the pipeline to start each one. Your output should look similar to the screenshot below.
Managing Web Bindings
It’s now time to start performing some common administrative tasks on the MyWebsite site. Of of the most common task is changing the web bindings.
When you created the MyWebsite site, you used the binding syntax of
*:8088:. This syntax represented listening on the IP address 127.0.0.1, on port 80 with no HTTP headers. Let’s say now you’ve decided that you instead want the site to listen on port 9000 instead. To do this, you’ll need to modify the site bindings.
Inspecting Existing Bindings
Before you can change the site’s bindings, you should first inspect the current configuration. One way to do that is to inspect the
Binding property returned by
Get-IISSite. Notice in the next code snippet how to do that. In the snippet,
Get-IISSite is querying the MyWebSite site, and only returning the
PS> (Get-IISSite -Name 'MyWebsite').Bindings
When you inspect just the
Bindings property, PowerShell will return an object with a few different pieces of information as shown below.
If you’d like to only see the binding information, you can specifically reference the
bindingInformation property as shown below.
If you’d like to change what port a site listens on, you must first remove the existing binding tying it to that port. To remove the binding, you first have to stop the site.
PS> Stop-IISSite -Name 'MyWebsite'
Once the site is stopped, you can now remove the existing binding tying the MyWebsite site to port 8088. To do that, you can use the
To remove the binding for MyWebsite as defined earlier, below you can see an example of using
Remove-IISSiteBinding to do just that. Below, you are providing the name of the site and specifying the binding syntax discovered earlier.
PS> Remove-IISSiteBinding -Name 'MyWebsite' -BindingInformation "*:8088:"
When you execute the code, you will be prompted to confirm your decision since you are doing something destructive as shown below.
There’s no limit to how many bindings you can have. You can just as easily have kept the original binding and added TCP port 9000 as a new binding.
Now you can run
Get-IISSite -Name 'MyWebsite' again. You’ll now see that the binding information has been removed as shown by the blank value under
Even though the site is published, IIS has no way of knowing how to route traffic to your site.
It is important to have at least one binding assigned to your site. To show you the importance, try to navigate to http://localhost:8088/index.html again. If you were using Mozilla Firefox web browser, you may see this message below.
Creating New Bindings
In the last section, you removed the only site binding MyWebsite had. While this broke the site, it’s only a temporary problem. It’s time to correct this issue by creating a new binding.
To create a new binding, use the
New-IISSiteBinding cmdlet. Since you need to bind port 9000 to the MyWebsite site now, specify the correct binding syntax as shown below.
PS> New-IISSiteBinding -Name 'MyWebsite' -BindingInformation "*:9000:"
Once you’ve created the new binding, start up the site again with
Start-IISSite. Recall that to create this new binding, IIS required the site to be brought down.
When the site starts again, you should now be able to access it on port 9000. To verify this, open your web browser of choice and navigate to http://localhost:9000. You should now see the webpage is working again under port 9000.
Removing a Website
This you just went through a tutorial and don’t intend to keep the MyWebsite site around for good, it’s time to clean it up. To remove a site, use the
Removing an IIS site can be a dangerous undertaking especially if you’re removing a site on IIS that’s hosting other production websites. To verify you’re removing the right site, it’s recommend to use the common
WhatIf PowerShell parameter. This parameter allows you to inspect what would have happen if the command would have actually run.
You can see an example of removing the MyWebsite site using the
WhatIf parameter below.
Remove-IISSite -Name 'MyWebSite' -WhatIf
If everything looks good, let go of the safety bar and re-run the code, this time without the
When using the
Remove-IISSitecmdlet, the site is not deleted. It is removed from the IIS configuration. Files for the website remain intact.
In this article, you’ve learned to create a website using the IISAdministration module. You’ve seen how to perform administrative operations necessary to maintain a website. You now know enough of the basics to work with any modern IIS host using PowerShell!