IIS Operations by Using PowerShell

We can automate the creation, modification, and deletion of websites in IIS using PowerShell scripts. I want to share some useful scripts to help you save time.

Loading the Web Administration Module

The first step is to load in the PowerShell module for IIS

Import-Module webadministration

2016-05-04_15-55-11

Creating a New File or Folder

New-Item is a quick and easy way to create a new file or folder on your computer. As an example, suppose you want to create a new directory named Windows PowerShell within the C:\Scripts folder. To do that call New-Item along with:

1) the full path to the new folder; and,

2) the new item type (which you can specify using the -type parameter and the value directory).

The command in question will look like this for the directory:

New-Item C:\SWTestAcademy -type directory

2016-05-04_16-15-28

The command in question will look like this for file:

New-Item c:\SWTestAcademy\onur_test.txt -type file

2016-05-04_16-29-58

Creating Web-Sites

New-Item is also used to create new Web-Sites within the IIS PowerShell, you can see Default Web Site below.

2016-05-04_16-40-53

Powershell Command:  

New-Item iis:\Sites\SWTestAcademySite -bindings @{protocol="http";bindingInformation=":80:SWTestAcademySite"} -physicalPath c:\SWTestAcademy

When you create a Web-Site need some additional parameters like the file system path and network bindings are needed to create a Web-Site.

2016-05-04_16-47-57

2016-05-04_16-47-13

Creating Web Applications

New-Item 'IIS:\Sites\SWTestAcademySite\SWTestApp' -physicalPath c:\SWTestAcademy -type Application

Specifying the -type parameter you tells the create an application

2016-05-04_17-07-14

2016-05-04_17-09-57

List Sites

You can get a list of websites running on the server by using the Get-Website cmdlet or by running Get-ChildItem

2016-05-04_23-29-17

This is another way to list them

2016-05-04_23-36-03

Deleting Sites

Here is how you delete the site using Powershell.

 Remove-Item IIS:\Sites\SWTestAcademySite

2016-05-04_17-12-24

2016-05-04_17-13-58

Thanks,
Onur Yazir

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.