In this short article, we’ll share some PowerShell commands that you can use to deploy and administrate your IIS server more efficiently.
Having a web server to publish online content is one of the most basic requirements for almost any business, whether it’s for marketing, online sales, internal documentation or some other kind of service.
Not long ago, we published an article explaining what IIS web server is and what it’s used for and we also published several tutorials: How to Install an IIS Web Server, How to Install IIS on Windows 10 and How to Install and Configure an FTP Server on Window (IIS). With all this information, we now think that this article on PowerShell is the perfect way to close the circle.
Throughout this article, we’ll explain how to install the server, deploy a website and perform some basic administrative tasks.
Installing the Web Server
Even though this isn’t exactly one of our usual tutorials, we’ll still take a minute to talk about the things you will need to be able to install an IIS web server using PowerShell commands. First of all, you will need to be registered with an organisation on the Jotelulu platform and have logged in. Additionally, you’ll need to have registered for a Servers subscription if you’re going to deploy the server on our infrastructure.
You’ll also need to have administrator privileges on the local machine that you’re using.
Before installing anything, we need to check whether any components have already been installed. To do this for IIS, run the following command:
# Get-WindowsFeature -Name *IIS*
This will basically produce a list of all the Windows features that have the term “IIS” in their name, with an “X” next to those that have already been installed. As you can see in the screenshot, there are two components:
- IIS Server Extension.
- WinRM IIS Extension.
Since there is no “X” between the square brackets in either case, these two components have not been installed. So, we can proceed to install the IIS web server and its management tools. To do this, run the following command:
# Install-WindowsFeature -name Web-Server -IncludeManagementTools
If you now run the check command again, you should see that the IIS features have now been installed. You can also check this by looking for IIS in the left-hand menu of the Server Manager.
# Get-WindowsFeature -Name *IIS*
Another way of checking that the service has been installed correctly is to open a web browser and try to connect to the localhost. To do this, type “127.0.0.1” in the address bar and hit Enter. If all has gone well, your screen should look like the screenshot below.
Check the Available PowerShell Commands
Whenever you install a new server or module, it’s always a good idea to check what new commands are available. To do this, simply run the following command:
# Get-Command -Module ‘IIS Administration’
In this case, the feature we’re interested in is “IIS Administration”, but you can replace this with whatever module you’re interested in. The command will produce a list of “cmdlet” commands that you can use to manage your services, and you can find out more information about these either on the system itself or on Microsoft’s Knowledge Base.
Setting Up a New IIS Site Using PowerShell Commands
Now, that you’ve installed IIS Web Server, you’ll be able to use PowerShell commands to deploy and manage a new website. To this, we’ll start with the “New-Item” command and create a new directory to contain all our website files. The full command will look something like this:
# New-Item -ItemType Directory -Name ‘<Directory>’ -Path ‘<Directory_Path>’
For this command:
- New-Item: is the command to create a new element.
- ItemType -Directory: identifies the new element as a directory.
- Name ‘<Directory>’: specifies the name of the new directory, where ‘<Directory>’ is the name.
- Path ‘<Directory_Path>’: specifies the complete path for the new directory, where ‘<Directory_Path>’ is the path.
# New-Item -ItemType Directory -Name ‘NachoTest’ -Path ‘C:\inetpub\wwwroot\NachoTest’
Then, you can also create a file to contain the HTML code for the website. To do this, we use the following command:
# New-Item -ItemType File -Name ‘index.html’ -Path ‘C:\inetpub\wwwroot\NachoTest\’
If you have multiple sites on the server, you may need to also configure the bindings to set each site to use a specific port. To do this, use the following command:
# New-IISSite -Name ‘<Site>’ -PhysicalPath ‘<Site_Path>’ -BindingInformation “*:<Ports_used>:”
- New-IISSite: is the command to create a new site.
- Name ‘<Site>’: specifies the name of the site.
- PhysicalPath ‘<Site Path>’: specifies the folder where the site files are contained.
- BindingInformation “*:<Ports used>:”: specifies the port that should be used to listen for requests.
Here’s an example of the full command:
# New-IISSite -Name ‘NachoTest’ -PhysicalPath ‘C:\inetpub\wwwroot\NachoTest\’ -BindingInformation “*:8080:”
With this command, you have now created and set up your new site.
At this point, you can also edit your newly created file “index.html” to include the necessary HTML code for your website, such as the following:
<title>IIS Test created with PowerShell</title>
<h1>IIS Test created with PowerShell</h1>
<p>We do this for <b>Jotelulu</b> tests.</p>
<p>Creating an IIS webpage using <b>PowerShell</b></p>
Save the file to apply the changes.
You now have a (very simple) functioning website that you can enhance with HTML, CSS, etc.
Starting and Stopping an IIS Site Using PowerShell Commands
You can also use PowerShell commands to start and stop your IIS Site or query its status. For example, if you use the command “Stop-IISSite”, you will be asked to confirm which site to stop. If you use the command “Stop-IISite -Name “NachoTest””, for example, explicitly stating the name of the site, you won’t be asked for any further information.
Similarly, if you run the command “Start-IISSite”, you will be asked to confirm the site to start, while entering “Start -IISSite -Name “NachoTest”” will start the site “NachoTest” without asking for more information.
If you want to know the status of your site, you can use the command “Get-IISSite”. Once again, you can specify the site to query up front by using the command “Get-IISSite – Name ‘<Site_Name>'”, changing <Site_Name> for the name of your IIS Site.
You can also query a specific property of the site, such as the listening ports (bindings). To do this, run the following command:
(Get-IISSite -Name ‘NachoTest’).Bindings
Deleting an IIS Site Using PowerShell Commands
The last operation we need to cover is deleting a site. To do this, use the command “Remove-IISSite”, adding the site name as parameter. When doing this, you actually have two options:
First, you can run the following command, which will delete the site in question:
# Remove-IISSite -Name ‘NachoTest’
Or, on the other hand, you can run the following command which will run a simulation of the command to check whether any problems could occur:
# Remove-IISSite -Name ‘NachoTest’ -WhatIf
That’s it! These are the most basic things you can do with IIS and PowerShell, but we’re sure to cover more tasks and commands in the future.
In this article, we have looked at the PowerShell commands that you can use to deploy and manage an IIS Server on Windows Server 2022, though they’re valid for other versions, such as Windows Server 2019 or Windows Server 2016. We’ve also explained how you can use PowerShell commands to install IIS, deploy a new website, start and stop the service and even delete a site.
If you’d like to find out more about IIS, check out the Microsoft website.
If you have any problems managing your services, please, don’t hesitate to get in touch. We’ll always do our very best to help.
Thanks for reading!