71

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?

wonea
  • 4,783
  • 17
  • 86
  • 139
Joey Green
  • 3,060
  • 3
  • 18
  • 14

5 Answers5

112

Just for future quick reference, the commands are:

Import-Module WebAdministration
Stop-WebSite 'Default Web Site'
Start-WebSite 'Default Web Site'
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 1
    I'm a total PowerShell noob. Do I need to import anything prior to the code you posted? What I tried was launching PowerShell then typing the first cmd you posted and PowerShell threw an error. – Joey Green Apr 29 '10 at 03:02
  • 1
    After reading the exception it said that I didn't need to Import WebAdministration but next it told me that Start-Website 'SiteName' as not a recognized cmdlet. – Joey Green Apr 29 '10 at 03:04
  • Where did you get the module from? – cpoDesign Sep 26 '13 at 10:48
  • It comes with PowerShell. – Keith Hill Sep 26 '13 at 15:13
  • This is perfect. I had a website that I needed to deploy in azure dev ops, and about 30% of the time it would fail to copy dlls to the iis site folder with the reason that it was being used by another process. I added a powershell task before and after the deploy step to stop and start the site and it worked like charm. Thanks. – Darth Scitus May 06 '20 at 17:06
20

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 
Tim
  • 333
  • 3
  • 7
  • It works but can someone explain the $args[0] part ? Thx – ToastMan Dec 13 '17 at 15:31
  • @ToastMan $args is the script block input argument array. [Docs for $args and other auto variables](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-5.1) – Tim Dec 14 '17 at 01:33
  • I needed the remote command. Thanks for pointing out that there is some additional work needed to make this happen! – Bonez024 Jan 08 '19 at 16:30
3

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Patrick S.
  • 31
  • 1
3

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 ()

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
Russ
  • 31
  • 1
0

Update IIS 10.0+ (2017+)

You can use the newer IISAdministration module with the Start-IISSite cmdlet like this:

Import-Module IISAdministration
Start-IISSite -Name "Default Web Site"

Or by using Get-IISSite, you can then start / stop like this:

Import-Module IISAdministration
$site = Get-IISSite "Default Web Site"
$site.Start()
$site.Stop()
KyleMit
  • 30,350
  • 66
  • 462
  • 664