Working with IIS 7.0 PowerShell provider

I have spent several hours with the new IIS 7.0 PowerShell provider, and I am very happy. I want to post a quick tip that I was looking for but did not find an answer to.

Suppose you want to write a script that is going to use the features that are loaded by the provider. The first thing that I would expect the script to do is to make sure that the provider is loaded into the environment, and then load it if needed.

So here is the chuck of PowerShell script that will do just that.

# Load the IIs Provider if it is not loaded already.
$foundSnapIn = $False
Get-PsSnapIn | ForEach-Object {
  if ($_.name -eq "IIsProviderSnapIn") {
    $foundSnapIn = $True
  }
}
if ($foundSnapIn -eq $False) {
  Write-Host "Adding IIS Snapin...."
  Add-PsSnapIn IIsProviderSnapIn
  Write-Host "Added."
}

Update 2008.12.10:
Here is a better way to handle this.

if (!(Test-Path IIS:\)) { Add-PsSnapIn IIsProviderSnapIn }

Tags: ,

One Response to “Working with IIS 7.0 PowerShell provider”

  1. Miha says:

    Thanks!

    Was looking for exactly that!

Leave a Reply