How can I stop and start individual websites in IIS using PowerShell?

WindowsIisPowershell

Windows Problem Overview


I have multiple sites configured in IIS7 on my Windows 7 development machine to run on the same port and usually only run one at a time depending on what I'm working on. I would like to be able to start and stop my development sites from PowerShell instead of having the IIS manager opened. Does anyone have a good resource to point me in the right direction or a script that already accomplishes this?

Windows Solutions


Solution 1 - Windows

Just for future quick reference, the commands are:

Import-Module WebAdministration
Stop-WebSite 'Default Web Site'
Start-WebSite 'Default Web Site'

Solution 2 - Windows

Adding to Keith's answer, you can perform this remotely using Invoke-Command.

Import-Module WebAdministration
$siteName = "Default Web Site"
$serverName = "name"
$block = {Stop-WebSite $args[0]; Start-WebSite $args[0]};  
$session = New-PSSession -ComputerName $serverName
Invoke-Command -Session $session -ScriptBlock $block -ArgumentList $siteName 

Solution 3 - Windows

To get access to system modules, Powershell needs to be run like this:

[path]\powershell.exe -NoExit -ImportSystemModules

I found the above on this iis forum.

Solution 4 - Windows

I found that the following to stop individual websites on a remote server to work:

Invoke-Command -Computername $servername -Scriptblock { 
    (Import-Module WebAdministration); 
    Stop-Website -Name "WebsiteName"; 
    Stop-Website -Name "AnotherWebsiteName"
}

I had some of the errors above until Import-Module was put in ()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoey GreenView Question on Stackoverflow
Solution 1 - WindowsKeith HillView Answer on Stackoverflow
Solution 2 - WindowsTimView Answer on Stackoverflow
Solution 3 - WindowsPatrick S.View Answer on Stackoverflow
Solution 4 - WindowsRussView Answer on Stackoverflow