powershelliisiis-10web-administrationhostheaders

How to remove multiple IIS bindings in PowerShell fast?


In a process of version upgrade, our current solution takes all bindings (except for two dummy urls) from one site and setting them on another site.

I'm currently removing the bindings through PowerShell but it is super slow. I've looked on about every thread in SO and almost every solution uses "Remove-WebBinding".

This is my current code:

Get-Website -Name $siteName | Get-WebBinding  | Where-Object { $_.HostHeader -notlike '*dummy*' } | Remove-WebBinding;

I have 272 (-2 dummy) bindings to remove and it takes more about 3 minutes.

Any ideas how to do it faster?

BTW: Adding all of those bindings one by one is super-slow too, but I guess if I'll find an answer here a similar solution would do for adding as well.


Solution

  • Copied from the comment and expand it a little bit.

    Cause of Slowness

    WebAdministration cmdlets were designed a long while ago, which has many disadvantages.

    The slowness you observed can be explained as by design. Though it is not open sourced, we can guess that each call of Remove-WebBinding creates the relevant underlying objects (like ServerManager) and then commits change to IIS configuration file. Thus, the more bindings to remove, the longer it takes (and the more resources consumed).

    Solution

    For all in-support IIS releases today (8+), you should use IISAdministration cmdlets instead. They are newly developed with both flexibility and performance in mind.

    By using a single ServerManager object and committing changes only once, removing bindings can be a lot faster.