Automating your website is easy with PowerShell and IIS. In this tutorial, you will learn how to customize scripts so that they can be repeated over and over again by creating triggers.
PowerShell and IIS are two powerful tools that can be used to automate websites. This tutorial will show you how to use these tools together, in order to list all sites on your website. Read more in detail here: powershell iis get list of websites.
You’ve probably dealt with Internet Information Services if you administer Windows Servers (IIS). Websites are one of IIS’s key capabilities, and using PowerShell IIS scripts, you can quickly manage and automate IIS!
You’ll learn about a new technique to administer IIS using PowerShell in this post. Using the concepts in this article, you may quickly develop and administer IIS websites using PowerShell IIS scripts.
One of two PowerShell modules – WebAdminstration or IISAdminstration – is required to handle IIS using PowerShell. The IISAdministration module, which is more recent, is covered in this article. If you’d like to see another option, see the companion video to this article, which shows how to use the older WebAdministration module.
Knowing how to handle even a basic website for your organization or the globe at large will set you apart from your colleagues. The parts that follow will teach you how to use IIS to develop and maintain a website. You’ll also learn how to use a few PowerShell cmdlets to manage web application pools.
Modules for IISAdministration vs. WebAdministration
When it comes to administering IIS, you have two alternatives. WebAdministration or IISAdministration are two options. Which one, though, should you choose?
The updated IISAdministration module provides a number of advantages over the previous WebAdministration module if you’re running IIS 10.0 or above.
Here are a few examples:
- IISAdministration cmdlets provide greater pipeline support and scaling.
- IISAdministration includes more efficient code than the earlier, traditional IIS WebAdministration cmdlets.
- The IISAdministration cmdlets are more straightforward and provide you direct access to the server management.
The emphasis of this study will be on the newer IISAdministration module.
Prerequisites
This is a hands-on article. If you want to follow along, make sure you have the following things ready.
The “Turn Windows features on or off” dialog box in Windows 10
It is suggested that you have a basic understanding of HTML. In the next instances, there will be a phase where HTML will be used. But don’t worry, the code will be easy to copy and paste!
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 all for the tools; now it’s time to look at the materials you’ll need to follow along.
IISAdministration Module Installation
After you’ve completed the requirements, you’ll need to do some setup work. Fortunately, the PowerShell module you’ll need (IISAdministration) is freely accessible in the PowerShell Gallery, so there’s not much preparation to perform.
Open an elevated PowerShell console on your web server and run the command below to install the module.
PS> Install-Module -Name ‘IISAdministration’
If you don’t have access to the internet, you may download the module to a different host and then transfer it to your modules directory. Take a look at the Save-Module command’s documentation.
Cmdlets That Are Available
Get-Command is one of the first cmdlets you should use to find a module’s cmdlet inventory, as it is with any PowerShell module you may not be acquainted with. You may see what’s available with this module by running Get-Command -Module ‘IISAdministration’ as shown below.
Despite the fact that you’ll be learning some basic website operations, these commands will give you an idea of what more you can perform.
Results from Get-Command -Module ‘IISAdministration’
You now have a list of cmdlets that are accessible. It’s time to start putting up a website.
Putting Together Your First Website
Let’s start by creating a webpage to work with. The first step is to establish a directory in which the web content will be saved. In order for IIS to appear, you’ll also need an HTML file in that directory. This directory will be used as the web directory in subsequent instances.
Making a Web Folder and a Web Page
To begin, create a directory. You may accomplish this using the New-Item cmdlet. The following code will create a directory in C:MyWebsite.
PS> New-Item -ItemType Directory -Name ‘MyWebsite’ -Path ‘C:’
With the aforementioned code, you can take whatever route you like. Generic routes will be utilized to make things simple.
After you’ve made the directory, you’ll need to create an HTML file that will serve as the home page for your new website.
As illustrated below, create a file named index.html in the C:Mywebsite directory.
PS> New-Item -ItemType File -Name ‘index.html’ -Path ‘C:MyWebsite’
Once the file has been generated, open it in your preferred editor and paste the code below into it. This HTML is a basic page that will be used to verify that IIS is correctly setup after you’re finished.
<!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 a Website in IIS
It’s now time to publish the newly built website to the rest of the globe. You’ll need to build a virtual directory for this. All of the assets and resources utilized for the page being served are stored in the virtual directory.
IIS generates a virtual directory under C:inetpubwwwroot by default. All of IIS’s default website assets and resources are located in this directory. You may also place your files in the default C:inetpubwwwroot directory, but it will not be utilized in these instances to keep the paths as short as possible. That’s why you’ve switched to C:MyWebsite.
Use the New-IISSite cmdlet to establish a new virtual directory (site). You may use this cmdlet to establish an IIS website.
Create a website named Mywebsite that points to the C:MyWebsite directory and listens on port 8088 for this article. You can see how to achieve this using the New-IISSite cmdlet in the code snippet below.
The BindingInformation parameter is the only part that might be unclear. Only binding syntax is allowed with this argument. New-IISSite will construct a website using an IP address (* is the same as 127.0.0.1), a TCP port to listen on, and any HTTP headers in this sample.
The syntax of binding may be described as protocol, followed by source: Port:destination is a combination of the words “port” and “destination.” In this example, you’ve set up IIS to accept HTTP requests on port 8088 from ANY () IP address, with the localhost as the destination.
PS> New-IISSite -Name ‘MyWebsite’ -PhysicalPath ‘C:MyWebsite’ -BindingInformation “*:8088:”
After the site has been generated, you may check it using Get-IISSite, as shown below.
In IIS, you may create a new website.
The website setup is begun by default, and your web server starts servicing HTTP requests. Navigate to the local computer using your preferred browser and port 8088. The website you made previously should now be visible.
In a web browser, visit the MyWebSite IIS site.
Congratulations! You’ve just used PowerShell to create your first website on IIS!
It’s time to learn how to administer your website and the IIS server that hosts it now that it’s up and running.
Taking Care of the IIS Website
Once a website is up and running, you will very certainly need to execute routine tasks such as pausing and restarting it. For example, if the site requires changes, you may need to restart it.
Start-IISSite and Stop-IISSite are two cmdlets you may use to administer the site in PowerShell. These two cmdlets function in the same way as the Stop-Service and Start-Service cmdlets do.
The process of shutting down a website is comparable to shutting down a Windows service. You’re blocking or allowing access to a site accessible through a protocol delivered by IIS on a single port. You are not, however, stopping the IIS host. Individual sites may be created and cancelled at any time.
Putting an End to a Single Website
The Shutdown-IISSite cmdlet is used to stop an IIS website. The site you built previously should be in the Started state by default. Run Halt-IISSite using the name as given below to stop the site and block access to MyWebsite. This code prevents IIS from serving the MyWebsite site. You will no longer be able to access the site after it has been stopped.
PS> Stop-IISSite -Name “MyWebsite”
The following is an example of evaluating the website’s status as Started, halting, then inspecting it again. In this scenario, you’re:
- Using Get-IISSite to get the status of the MyWebsite site.
- Stop the MyWebsite site using the Stop-IISSite cmdlet. This cmdlet closes the website’s access. You will be asked to confirm this action since it involves operational changes.
- By detecting the Stopped status, run Get-IISSite again to confirm that MyWebsite site is stopped.
Obtaining, halting, and checking the status of MyWebsite
All Sites Must Be Shut Down
You blocked a single site above, but you can simply stop all sites as well. PowerShell will enumerate all sites and stop them all by utilizing the pipeline and passing the output of Get-IISSite to Stop-IISSite. The optional Verbose switch argument is used to display extra information in the code sample below.
-Verbose Get-IISSite | Stop-IISSite
Using PowerShell to stop all IIS sites
Stopping the W3SVC, or World Wide Web Publishing Service, in IIS will also halt all sites. Stopping the Windows service stops IIS from running and stops all sites from being served. However, the sites may still seem to be in the Started stage if you do this.
Creating a Single Web Page
Assume you’ve made some modifications to the MyWebsite site and now need to restart it. The Start-IISSite command must now be used. Simply give the name in the same way you did with the Stop-IISSite cmdlet.
PS> Start-IISSite -Name ‘MyWebsite’
You can then use Get-IISSite to check the status once it has been run. When you’ve done that, you should see something like the screenshot below.
Get-IISSite is used to check the status of MyWebsite, while Start-IISSite is used to start the site.
If you reload your browser, your site should now be visible at http://localhost:8088.
All Sites Should Be Started
You may utilize the pipeline to start all websites on your IIS host in the same way that you stopped all sites from being published.
PS> Get-IISSite | Start-IISSite
You’re obtaining all of the IIS sites that are currently being served and utilizing the pipeline to start each one. Your final product should like the image below.
To launch all sites on the host, use the pipeline.
Taking Care of Web Bindings
On the MyWebsite site, it’s now time to begin conducting some standard administrative activities. Changing the web bindings is one of the most frequent tasks.
You used the binding syntax of *:8088: while creating the MyWebsite site. This syntax represents listening on port 80 with no HTTP headers on the IP address 127.0.0.1. Let’s pretend you’ve changed your mind and now want the site to listen on port 9000. You’ll need to change the site bindings to do this.
Examining Current Bindings
You should verify the existing setup before changing the site’s bindings. Examining the Binding property supplied by Get-IISSite is one approach to do so. You’ll see how to accomplish that in the following code line. Get-IISSite is querying the MyWebSite site in the sample and only returning the Bindings property.
PS> (Get-IISSite -Name ‘MyWebsite’).Bindings
PowerShell returns an object with a few distinct bits of information when you view simply the Bindings property, as illustrated below.
Example 1: Obtaining bound data
If you simply want to display the binding information, you may use the bindingInformation property, which is illustrated below.
Example 2: Obtaining binding data
Bindings Must Be Removed
If you want to alter the port on which a site listens, you must first delete the binding that binds it to that port. You must first stop the site in order to remove the binding.
PS> Stop-IISSite -Name ‘MyWebsite’
You may now delete the current binding linking the MyWebsite site to port 8088 after the site has been terminated. Use the Remove-IISSiteBinding cmdlet to do this.
Below is an example of using Delete-IISSiteBinding to remove the binding for MyWebsite that was previously established. You’ll enter the site’s name and the binding syntax you learned previously in this section.
PS> Remove-IISSiteBinding -Name ‘MyWebsite’ -BindingInformation “*:8088:”
When you run the code, you’ll be asked to confirm your choice since you’re about to do something bad, as illustrated below.
Using the Verbose option to remove site binding
You can have an unlimited number of bindings. You could have just as easily preserved the previous binding and created a new one for TCP port 9000.
You may now execute Get-IISSite -Name ‘MyWebsite’ once again. The binding information has now been erased, as seen by the blank value under Bindings.
Verifying that the previous bindings have been deleted
Despite the fact that your site has been published, IIS has no means of knowing how to direct traffic to it.
It is critical that your site has at least one binding allocated to it. Try going to http://localhost:8088/index.html again to see how important it is. This notice may appear if you were using Mozilla Firefox as your web browser.
I’m unable to make a connection.
Making New Bindings is a process that involves the creation of new bindings.
You eliminated MyWebsite’s single site bound in the previous step. While the site was temporarily down as a result of this, it is simply a temporary issue. It’s time to create a new binding to fix this problem.
Use the New-IISSiteBinding cmdlet to create a new binding. Because you need to connect port 9000 to the MyWebsite site right now, use the code given below.
PS> New-IISSiteBinding -Name ‘MyWebsite’ -BindingInformation “*:9000:”
Start the site using Start-IISSite once you’ve built the new binding. Remember that IIS requires the site to be taken down in order to generate this new binding.
You should now be able to access the site on port 9000 when it restarts. To confirm this, go to http://localhost:9000 in your preferred web browser. You should now be able to access the website using port 9000.
The website is the same, but the TCP port is different.
Taking Down a Website
It’s time to clear up the MyWebsite site now that you’ve completed a lesson and don’t want to keep it around indefinitely. Use the Delete-IISSite cmdlet to remove a site.
Removing an IIS site might be risky, particularly if it’s one that’s hosting other production websites. It’s a good idea to utilize the common WhatIf PowerShell option to make sure you’re eliminating the correct site. This argument lets you see what would have happened if the command had been executed.
The WhatIf argument is used to remove the MyWebsite site in the example below.
-WhatIf Remove-IISSite -Name ‘MyWebSite’
Using -WhatIf to be cautious with damaging cmdlets!
If everything seems to be in order, release the safety bar and rerun the code without the -WhatIf argument.
The site is not destroyed when using the Remove-IISSite cmdlet. It is no longer present in the IIS settings. The website’s files are still intact.
Summary
You’ve learnt how to use the IISAdministration module to construct a website in this tutorial. You’ve seen how to execute the administrative tasks required to keep a website up to date. You now have enough knowledge of the fundamentals to use PowerShell to interact with any contemporary IIS host!
Additional Reading
The “powershell create iis site if not exists” is a command that allows you to use PowerShell and IIS to automate websites. This command will create an empty website if it does not exist.
Frequently Asked Questions
Can PowerShell be used for automation?
A: Yes PowerShell can be used to automate tasks in Windows.
How do I set up automate in IIS?
A: In order to configure the IIS website, you need to access the Windows PowerShell. To open it up on your computer, go into Start and type in PowerShell and then press enter. Then type in IIS and click Enter/Return on your keyboard.
How do I create a website using IIS PowerShell?
A: You might want to use Visual Studio Code for this.
Related Tags
- deploying a net web application to iis with powershell
- powershell iis commands
- iis application pool advanced settings powershell
- powershell script for iis configuration
- powershell stop-iis site confirm