title: PowerShell: Load WebAdministration in ps1 script on both IIS 7 and IIS 7.5
body: <p>I have a PowerShell script that configures web site and web application settings in IIS. So I use the cmdlets in the WebAdministration snap in.  But this script needs to run on Windows 2008, Windows 7 and Windows 2008 R2.  And I need it to run with minimal fuss from the user.</p>
  
  <p>The problem is that Windows 7 and Windows 2008 R2 use IIS 7.5 which comes with the WebAdministration installed as a module.  On Windows 2008 we have installed the IIS 7 PowerShell provider which installs WebAdministration as a snap in.</p>
  
  <p>So including <strong>import-module WebAdministration</strong> in the script blows up on IIS 7 but works fine on IIS 7.5 and including <strong>add-pssnapin WebAdministration</strong> blows up on IIS 7.5 but works fine on IIS 7.</p>
  
  <p>So our workaround is to make the administrators load WebAdministration manually with the appropriate command for the environment before running the script.  But this isn't optimal as it is easy to forget which command works in which environment.  We could create two different scripts, but that creates a maintenance problem for development.</p>
  
  <p>Has anyone solved this problem?  Does anyone know how to check the environment and then call the appropriate cmdlet from within the PS script?</p>
  
  <p>--- ANSWER (for my situation) ----</p>
  
  <p>The solution is a combination of code and pre-configured console.  The IIS 7 PoSH Provider includes a desktop shortcut that launches a PoSH console with WebAdministration loaded.  That combined with the following function make my script run like a charm on all three systems.</p>
  
  <pre><code>Function Load-WebAdmin {
    $webAdminModule = get-module -ListAvailable 
                                          | ? { $_.Name -eq "webadministration" }
    If ($webAdminModule -ne $null) {
      import-module WebAdministration
    }
  }
  </code></pre>