Back to Menu
Connecting You To Opportunity
What can we help you find?
| Login|Sign Up
Back to Menu
Hello
  • Login
  • Sign Up

How To Manage IIS Websites With PowerShell

By
Adam Bertram
,
business.com writer
|
Sep 06, 2018
Home
> Technology
SHARE THIS

When setting up a new website in IIS are you still using the IIS Web Manager?

When setting up a new website in IIS are you still using the IIS Web Manager? If so, here's a much more scalable, reliable and faster way.

By using the WebAdminstration module and the Invoke-Command cmdlet in PowerShell you can just as easily manage 1, 100 or 1,000 different websites on an IIS server. Let's see how we can make this a reality.

First steps

First, you'll need to ensure you've got IIS installed on your Windows Server. We'll be demonstrating these steps on a Windows Server 2012 R2 machine, but this should work the same on Windows Server 2008 R2 as well.

Next, you'll need to ensure you've got PowerShell remoting configured and the appropriate firewall ports allowed on the server. The quickest way to do to this is by running winrm quickconfig on the server. This will go through all of the prerequisite steps to make PowerShell remoting available.

When IIS is installed, it comes with a PowerShell module called WebAdministration. This module gives you many different cmdlets for managing IIS. You can see we've got six cmdlets specifically dedicated to managing websites.

Let's see if we have any websites on our IIS server.

Get-Website

It looks like we have the default website as well as a couple other site for our PowerShell Desired State Configuration setup. We'd like to create another one for a web application we're building. We'll first need to create a folder, then use the New-WebSite cmdlet to specify a name for the website and where to place it on the filesystem. Notice, by default, it creates the website as stopped and sets a HTTP binding on port 80.

New-Website –Name MyWebApp –PhysicalPath C:\inetpub\MyWebApp

Since I want to use this website, we'll need to start it.

Get-Website –Name MyWebApp | Start-Website

Oops! We got an error. This error means that we have a conflicting binding. Let's see which website is doing that. Recall that it was creating the site with a HTTP binding on port 80.

Get-Website |
            Where {($_.bindings.collection.protocol –eq ‘http’) –and
                        ($_.bindings.collection.bindingInfo –eq ‘*:80:’)
}

It looks like the Default Website is using the same binding. Let's stop the default website.

Get-Website –Name ‘Default Web Site’ | Stop-Website

Now, let's try to start up our MyWebApp website again. You'll see that you no longer receive that error message. We now have a website called MyWebApp started up and ready to go.

PowerShell IIS Drive

By now you've seen how to use the cmdlets to manage websites but we haven't talked about another useful feature that comes with the WebAdministration module called the PowerShell IIS drive. When you import the WebAdministration module, PowerShell builds a drive called IIS. This drive allows you to manage IIS just like you would via the file system by simply using IIS: instead of C: to represent the drive.

To demonstrate this drive, instead of using Remove-Website to remove this website let's simply use Remove-Item and the IIS drive.

You can see that the IIS drive makes removing a website just as easy as removing a file! The IIS drive is a handy way to manage lots of different configuration items in IIS.

You've seen that managing websites is very easy with PowerShell. We were working on a single server but by applying the techniques you learned in this article, you can now replicate this on many servers at once. For example, maybe you'd like to create a website called MyWebApp on a list of servers in a text file. No problem!

Get-Content –Path ‘C:\Servers.txt’ | Invoke-Command –ScriptBlock {
    Import-Module WebAdministration
    New-Website –Name MyWebApp –PhysicalPath
C:\inetpub\MyWebApp
    }

All it takes is to read each server name in a text file, take our existing code to build a website and place that into a script block which we then pass to the Invoke-Command cmdlet to run it remotely on each server.

The next time you launch the IIS manager think to yourself, "Can I do this in PowerShell?" If you can, do it. It will save you tons of time!

Adam Bertram
Adam Bertram
Adam Bertram is a 20-year veteran of IT and experienced online business professional. He's an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram or the TechSnips Twitter account at @techsnips_io.
Like the article? Sign up for more great content.Join our communityAlready a member? Sign in.
We'd love to hear your voice!
Login to comment.
LoginSign Up