title: How to Stop IIS with PowerShell?
body: <p>I am learning PowerShell and trying to stop IIS on a remote server.</p>
  
  <p>I am  using PowerGUI Script Editor that I started in admin mode.</p>
  
  <p>I have this code</p>
  
  <pre><code>$service = Get-WmiObject Win32_Service -ComputerName 'myserver'  -Filter "Name='IISAdmin'"
  $service
  $service.StopService();
  $service.State
  </code></pre>
  
  <p>State always comes back as running. I don't know why it does not stop.</p>
  
  <p>Edit </p>
  
  <p>Error I get when running</p>
  
  <pre><code>Invoke-Command -ComputerName 'myserver' { Stop-Service IISAdmin }
  </code></pre>
  
  <blockquote>
    <p>Connecting to remote server failed with the following error message :
    The client cannot connect to the destination specified in the request.
    Verify that the service on the destination is running and is
    accepting requests. Consult the logs and documentation for the
    WS-Management service running on the destination, most commonly IIS or
    WinRM. If the destination is the WinRM service, run the following
    command on the destination to analyze and configure the WinRM service:
    "winrm quickconfig". For more information, see the
    about_Remote_Troubleshooting Help topic.</p>
  </blockquote>
  
  <p>Edit2</p>
  
  <p>I found this and it seems to work. I don't know how to get the information back from Stop-Service so I had to use the other way to get it to me. If you know how please let me know.</p>
  
  <pre><code>Stop-Service -Force -InputObject $(Get-Service -Computer myserver -Name IISAdmin)
  $service = Get-WmiObject Win32_Service -ComputerName 'myserver '  -Filter "Name='IISAdmin'"
  $service.State
  </code></pre>
  
  <p>This works. I don't understand why. The only thing I can think off is I had to use -Force as it would not stop the server otherwise so maybe that is a reason?</p>