Guy recommends :
Free Permissions
Analyzer Tool

Solarwinds Free Download of Permissions Analyzer

 

View the effective permissions for a folder or shared drive. Free download try it now!


Stop Windows Services with PowerShell

Windows PowerShell Stop-Service Cmdlet

This page will show you how to stop a Windows service.  If you prefer we can easily modify the script to Restart the service.  In order to get a grounding in the PowerShell syntax associated with this 'Service' family of commands, I suggest that you begin with my Get-Service page.

PowerShell's Stop-Service Topics

 ♣

Preliminary Script to List Services Which Are Running

Stopping the wrong service could have disastrous effects on a server especially if you append the -force parameter.  This is why I suggest that you run this script and select a less important service such as Bits, Spooler or Themes

# PowerShell script to list running services
Clear-Host
Get-WmiObject Win32_Service `
| Where-Object {$_.State -eq 'Running'} `
| Format-Table name, state, status -auto

Learning Points

Note 1:  To highlight the modular nature of this script I have written it on three lines.  The first pipeline gets a list of Windows services, the second pipeline uses a where statement to filter Running services, while the final line merely formats the output.

Example 1: How to Stop a Windows Service

I have chosen the Themes service as a vehicle to test the stop service command.  You may wish to substitute a different value for $SrvName.

# PowerShell cmdlet to stop the named service "Themes"
Clear-Host
$SrvName = "Themes"
$SrvName + " is now " + (Get-Service $SrvName).status
Stop-Service $SrvName
$SrvName + " is now " + (Get-Service $SrvName).status

Note 2:  Naturally, for a production script you could simplify to:

# Production script to stop a service
Stop-Service -name "Themes"

Note 3:  To restart a service, simply change the verb from Stop to Restart thus:

# Production restart service
Clear-Host
Restart-Service -name "Themes"

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v10

SolarWinds' Network Performance Monitor will help you discover what's happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds' Network Performance Monitor

Example 2: Stop Service Safety Procedure

Before you stop a service, especially if this is the first time and you are not familiar with dependencies, I suggest you run this script.

# PowerShell script to check service dependencies
#Clear-Host
Get-Service | Where-Object {$_.DependentServices.count -gt 0} | Format-Table` Name, @{Label="Number"; Expression={$_.dependentservices.count}} -auto

Note 4:  This script employs Get-Service (rather than Stop-Service) to lists all services with more than one dependent.  See more on the $_ variable.

Example 3: Stop Service - Force Parameter

# PowerShell cmdlet to force a service to stop
Clear-Host
$SrvName = "Themes"
$SrvName + " is now " + (Get-Service $SrvName).status
Stop-Service $SrvName -force
$SrvName + " is now " + (Get-Service $SrvName).status

Note 5:  While -force probably is not needed for the Themes service, there maybe times when you need this power.  However, don't abuse the -force parameter or  else your server may stop functioning in the way that you intended.

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer's Toolset v10

This Engineer's Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it's like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer's Toolset now!

Download your fully functional trial copy of the Engineer's Toolset v10

More Research for Stop-Service Parameters

Before we use the -force parameter, let us see how we can research all the parameters for a cmdlet.

# Research Stop-Service parameters
Clear-Host
Stop-Service -full

Note 6:  Interesting parameters include -force

Note 7:  For Dependencies, research the sister cmdlet Get-Service

Stop Service Example

Here is an example designed to stop a service, but with a built-In wait command before continuing.

Scenario: You want a script to stop a service then shutdown the computer.

# This script stops the Print Spooler service.
# However, it will wait while the service is stopping ....

$ServiceName = 'Print Spooler'
Stop-Service $ServiceName
Write-Host $ServiceName'...' -NoNewLine
$TestService = Get-Service $ServiceName
While($TestService | Where-Object {$_.Status -eq 'Running'}){
Write-Host '.'-NoNewLine
Start-Sleep 3
}
Write-Host "`n$ServiceName stopped"

Note 8: To see the effect alter $ServiceName to $ServiceNamexxx.

See also Alert Central review »

Guy Recommends: SolarWinds Network Topology Mapper (NTM)SolarWinds Network Topology Mapper

NTM will produce a neat diagram of your network topology.  But that's just the start; Network Topology Mapper can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you test drive the Network Topology Mapper then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download your 14 day free trial of SolarWinds Network Topology Mapper

Remoting with Services

One aspect of remoting in PowerShell v 2.0 is simply to append -computerName xyz to the command that you ran on the local machine.  For further research try:

Clear-Host
Get-Command | Where { $_.parameters.keys -Contains "ComputerName"}

Surprise!  Get-Service is amongst the cmdlets that support remoting, but stop, start and Restart-Service are not on the list.  More bad news, stop, start and Restart-Service really don't work on network machines.  Thus you have to employ different techniques such as Get-WmiObject and InvokeMethod.  Alternatively, you could enter-PSSession and then run Restart-Service as if you were on the local machine.  However, to get that working you have to first install and setup WinRM

The Service Family (Each member has a different verb)

Get-Service:  Useful for listing the services
Set-Service:  Crucial parameter -startuptype
Start-Service:  The verb 'start' says it all
Stop-Service:  Handy for scripts which prevent unwanted services running e.g. Telnet
Restart-Service:  A nice touch by the creator's of PowerShell; this cmdlet removes the need to explicitly stop then start the service.

Summary of PowerShell's Stop-Service Cmdlet

This page will show you how to stop a Windows service.  If circumstances demand, then you could modify the script to force a stop even thought there are dependent services.

If you like this page then please share it with your friends

 


See more PowerShell examples of process and service

PowerShell Home  • Get-Process  • Stop-Process  • PowerShell Start-Process  � Set-Service

Get-Service  • Start-Service  • Stop-Service  • Restart-Service   • Free WMI Monitor

PowerShell Start-Sleep  • Get-WmiObject win32_service   • Windows PowerShell

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2013 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: