Working with bindings on Windows Web Server IIS with PowerShell

Short post today about how you can get, add and remove bindings with PowerShell on Windows Server running IIS (Internet Information Server). I find it more effective to add bindings through PowerShell rather than clicking through the user interface, especially if a website contains a lot of bindings and you have to scroll and look through the list manually to find the binding you want to delete.

In this case I created three work tools for getting bindings, adding new bindings, and remove a binding.

The different tools

Below a give a short description of what the tools does, and the results from the scripts. There might be some cases that can crash the script that have not been tried out or experienced, but feel free to correct and reuse the tools as you need. Remember that the scripts must be loaded into memory before you can use them, for instance by dot source at the location of the file . .Get-WebsiteBinding.ps1 or by playing the script in VS Code or PowerShell ISE.

List all bindings on a website

The first script it uses the Get-WebBinding from the webadministration module to retrieve either a specified binding by using the parameter -DomainName or list all bindings by using the switch parameter -All. Below the script, you can see how the script behaves and the result.

function Get-WebsiteBinding {
    [CmdletBinding()]
    param (
        # Name of Website
        [Parameter(Mandatory = $true)]
        [string]
        $WebsiteName,
        # Name of domain
        [Parameter(Mandatory = $false)]
        [string]
        $DomainName,
        # List all bindings
        [Parameter(Mandatory = $false)]
        [Switch]
        $All
    )
    process {
        if ($All -eq $true) {
            Get-WebBinding -Name $WebsiteName
        }
        elseif ($All -eq $false) {
            Write-Verbose "Retriving $DomainName from the website $WebsiteName.."
            Get-WebBinding -Name $WebsiteName | Select-Object Bindinginformation | Where-Object { $_.Bindinginformation -like "*$DomainName*" }
        }
    }
}

The result of the cmdlet Get-WebsiteBinding. It lists out either all existing bindings by using the -All parameter or by specifying binding using the parameter -DomainName.

    # Running with the -All parameter
    PS C:\> Get-WebsiteBinding -WebsiteName "WebBindingsDemo" -All

    protocol bindingInformation  sslFlags
    -------- ------------------  --------
    http     *:80:example.fe            0
    http     *:80:example.fen           0
    http     *:80:example.dk            0
    http     *:80:example.no            0
    http     *:80:example.nl            0
    http     *:80:www.example.io        0
    http     *:80:www.example.dk        0
    http     *:80:www.example.no        0

When using the -DomainName parameter you retrieve all occurrences of the domain, as you can see below it retrieves bot with and without www.

# Running with the -DomainName parameter
PS C:\> Get-WebsiteBinding -WebsiteName "WebBindingsDemo" -DomainName "example.no"

bindingInformation
------------------
*:80:example.no
*:80:www.example.no

Add new binding to a website

The second script uses the New-WebBinding, also from the webadministration module. When running the script you can specify the same parameters as New-WebBinding, in addition you get prompted if you want to add or not, and confirms newly added binding by outputting it to console.

function New-SingleWebsiteBinding {
    [CmdletBinding()]
    param (
        # Name of Website
        [Parameter(Mandatory = $true)]
        [string]
        $WebsiteName,

        # Name of domain to add
        [Parameter(Mandatory = $true)]
        [string]
        $DomainName,

        # Port
        [Parameter(Mandatory = $false)]
        [UInt32]
        $Port = 80,

        # IP Address
        [Parameter(Mandatory = $false)]
        [String]
        $IPAddress = "*",

        # Type
        [Parameter(Mandatory = $false)]
        [String]
        $Protocol = "http"
    )
    process {

        $Answer = Read-Host "`nWant to add $DomainName to $($WebSiteName)? Y/N"

        if ($Answer -eq "Y") {

            Write-Host -ForegroundColor Yellow "Adding $DomainName to $WebsiteName..."
            New-WebBinding -Name $WebsiteName -HostHeader $DomainName -Port $Port -IPaddress $IPaddress -Protocol $Protocol

            Get-WebBinding -Name $WebSiteName -HostHeader $DomainName
            sleep 2
            Write-Host -ForegroundColor Green "Adding $DomainName to $WebsiteName completed..."
        }
        elseif($Answer -eq "N")
        {
            Write-Host -ForegroundColor Yellow "Adding $DomainName to $WebsiteName aborted..."
        }

        else {
            Write-Host -ForegroundColor Yellow "Wrong input, Type Y or N.."
        }
    }
    }

The results when running the script looks like below, the first if you choose yes to add a new binding, second if you choose no, and the third if you type in other answers than Y or N.

# Prompting Y to add binding
PS C:\> New-SingleWebsiteBinding -WebsiteName "WebBindingsDemo" -DomainName "www.example.com"

Want to add www.example.com to WebBindingsDemo? Y/N: Y
Adding www.example.com to WebBindingsDemo...

Adding www.example.com to WebBindingsDemo completed...
protocol bindingInformation   sslFlags
-------- ------------------   --------
http     *:80:www.example.com        0
# Prompting N to rethink
PS C:\> New-SingleWebsiteBinding -WebsiteName "WebBindingsDemo" -DomainName "www.example.com"

Want to add www.example.com to WebBindingsDemo? Y/N: N
Adding www.example.com to WebBindingsDemo aborted...
# Prompting anything but Y or N you get error with wrong input.
PS C:\> New-SingleWebsiteBinding -WebsiteName "WebBindingsDemo" -DomainName "www.example.com"

Want to add www.example.com to WebBindingsDemo? Y/N: maybe
Wrong input, Type Y or N..

Remove binding from a website

Third and last script. This script uses the Remove-WebBinding from the webadminitration module also and is the base command of the script. It removes a single binding. Note: in this version of the script it removes all occurrences of the binding, let’s say you have example.com:443 and example:80 it will remove both occurrences.

function Remove-SingleWebSiteBinding {
    [CmdletBinding()]
    param (
        # Name of Website
        [Parameter(Mandatory = $true)]
        [string]
        $WebsiteName,
        # Name of domain name
  [Parameter(Mandatory = $true)]
        [string]
        $DomainName
    )
    process {
        $Answer = Read-Host "`nDo you want to delete $DomainName from $($WebSiteName)? Y/N"
        if ($Answer -eq "Y") {
            Write-Host -ForegroundColor DarkYellow "Deleting $DomainName from $($WebSiteName)..."
            Remove-WebBinding -Name $WebsiteName -HostHeader $DomainName
            Sleep 2
            Write-Host -ForegroundColor Green "Deletion completed, listing rest of bindings..."
            Get-WebBinding -Name $WebSiteName
        }
        elseif ($Answer -eq "N") {
            Write-Host -ForegroundColor Yellow "Deletion aborted, exiting..."
        }
        else {
            Write-Host -ForegroundColor Cyan "Type Y or N, exiting..."
        }
    }
}

The results from the Remove-SingleWebSiteBinding behaves much like the the New-SingleWebSiteBinding only the opposite effect. When removing a binding you get prompted if you want remove the binding, if you say Y it will remove all occurrences of the specified binding, if you say N the process aborts, and if you type anything other than Y or N, then the process aborts with a message that you must type Y or N.

# Prompting Y to remove binding
PS C:\> Remove-SingleWebSiteBinding -WebsiteName "WebBindingsDemo" -DomainName "www.example.com"

Do you want to delete www.example.com from WebBindingsDemo? Y/N: Y
Deleting www.example.com from WebBindingsDemo...
Deletion completed, listing rest of bindings...

protocol bindingInformation  sslFlags
-------- ------------------  --------
http     *:80:example.fen           0
http     *:80:example.dk            0
http     *:80:example.fl            0
http     *:80:example.nl            0
http     *:80:www.example.io        0
http     *:80:www.example.dk        0
http     *:80:www.example.fl        0
# Prompting N to remove binding
PS C:\> Remove-SingleWebSiteBinding -WebsiteName "WebBindingsDemo" -DomainName "www.example.com"

Do you want to delete www.example.com from WebBindingsDemo? Y/N: N
Deletion aborted, exiting...
# Prompting maybe to remove binding
PS C:\> Remove-SingleWebSiteBinding -WebsiteName "WebBindingsDemo" -DomainName "www.example.com"

Do you want to delete www.example.com from WebBindingsDemo? Y/N: maybe
Type Y or N, exiting...

These scripts can absolute be improved and adjusted to match other needs, but for my use at the current moment I find them useful and it takes less time managing websites with a lot of bindings.

Thanks for reading, have an awesome week!