Powershell update IIS Bindings Powershell update IIS Bindings powershell powershell

Powershell update IIS Bindings


The steps to UPDATE a binding in a website's binding collection, the process is actually to Get the website's binding collection, modify the specific binding, and then set the whole collection again.

Function ReplaceWebsiteBinding {    Param(        [string] $sitename,        [string] $oldBinding,        [string] $newValue    );    import-module webadministration;    $wsbindings = (Get-ItemProperty -Path "IIS:\Sites\$sitename" -Name Bindings)    for($i=0;$i -lt ($wsbindings.Collection).length;$i++){        if((($wsbindings.Collection[$i]).bindingInformation).Contains($oldBinding)){            ($wsbindings.Collection[$i]).bindingInformation = $newValue;        }    }    Set-ItemProperty -Path "IIS:\Sites\$sitename" -Name Bindings -Value $wsbindings}ReplaceWebsiteBinding "Default Web Site" "*:80:" "192.168.1.101:80:SomeHostHeader.domain";

[note: Edited 20131016: Clean up answer for easier viewing ][note: Edited 20160817: Corrected param variable type definitions ]


Here's another syntax form. I prefer this one because it seems more natural. If you don't already have the webadministration PS module loaded, import that first (import-module webadministration).

New-WebBinding -name test03 -port 443 -Protocol https -HostHeader test03.int -IPAddress "*"


Here is a Powershell script I wrote recently that can be adapted to do what you want:

# Updates IIS bindings across all sites by replacing all occurrences# of $searchString for $replaceString in the binding host header.# Note that the search and replace is case insensitive.$searchString = "ovh-ws0"$replaceString = "ovh-ws1"foreach ($website in Get-Website) {    "Site: {0}" -f $website.name    $bindings = Get-WebBinding -Name $website.name    foreach ($binding in $website.bindings.Collection) {        $bindingInfo = $binding.bindingInformation        "    Binding: {0}" -f $bindingInfo        if ($bindingInfo -imatch $searchString) {            $oldhost = $bindingInfo.Split(':')[-1]            $newhost = $oldhost -ireplace $searchString, $replaceString            "        Updating host: {0} ---> {1}" -f $oldhost, $newhost            Set-WebBinding -Name $website.name -BindingInformation $bindingInfo -PropertyName "HostHeader" -Value $newhost        }    }}

And this is a sample output from the script above:

Site: alpha    Binding: 100.101.102.103:80:alpha.redacted.com    Binding: 100.101.102.103:80:ovh-ws0-alpha.redacted.com        Updating host: ovh-ws0-alpha.redacted.com ---> ovh-ws1-alpha.redacted.com    Binding: 100.101.102.103:443:ovh-ws0-alpha.redacted.com        Updating host: ovh-ws0-alpha.redacted.com ---> ovh-ws1-alpha.redacted.com    Binding: 100.101.102.103:443:alpha.redacted.comSite: beta    (etc)Site: release    (etc)

Of course, the script can be adapted to do modify bindings in other ways. For example, the following script will update the IP addresses:

# Updates IIS bindings across all sites by replacing all IP addresses from $oldIP to $newIP.$oldIP = "100.101.102.103"$newIP = "*"foreach ($website in Get-Website) {    "Site: {0}" -f $website.name    $bindings = Get-WebBinding -Name $website.name    foreach ($binding in $website.bindings.Collection) {        $bindingInfo = $binding.bindingInformation        "    Binding: {0}" -f $bindingInfo        if ($bindingInfo -imatch $oldIP) {            "        Updating IP: {0} ---> {1}" -f $oldIP, $newIP            Set-WebBinding -Name $website.name -BindingInformation $bindingInfo -PropertyName "IPAddress" -Value $newIP        }    }}