Last Updated: February 25, 2016
·
5.635K
· jgemedina

PowerShell script to restart a Web Site and App Pool

This script will first verify that the proper PowerShell module is installed in the System, if not it will warn the user and exit.

if (-not(get-module -name "WebAdministration"))
{
      if (-not(get-module -listavailable | where-object { $_.Name -eq "WebAdministration" }))
      {
            write-host "Web Administration Module not available, exiting..." -foreground red
      }
      else
      {
            write-host ">> Attempting to restart My Website..." -foreground cyan
            write-host ""

            import-module "WebAdministration"
            stop-website MyWebsite
            restart-webapppool MyWebsite
            start-website MyWebsite

            write-host ">> Done, check output for errors or hit any key to close this Window." -foreground cyan -nonewline
            read-host
      }
}