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.
In the case of Windows 7 and Server
2008, you don't need to download any extra files, just 'Add Feature' Windows PowerShell.
However, for older operating systems, installing can be confusing because
there are different versions of PowerShell for XP, Windows Server 2003 and
Vista. For such legacy systems only, you need to
download PowerShell from Microsoft's site.
Once you have installed PowerShell 2.0, I recommend choosing the
ISE (Integrated Scripting Engine) version, it
will save you buying a text editor.
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.
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"
Encouraging computers to sleep when not in use is a great idea -
until you are away from your desk and need a file on that remote sleeping machine!
Wake-On-LAN really will save you that long walk to awaken a hibernating
machine; however my reason for encouraging you to download this utility is
just because it's so much fun sending those 'Magic Packets'. As Wake-On-LAN (WOL) is free, see
if I am right, and you get a kick from arousing those sleeping machines.
WOL also has business uses for example, wakening machines so that they can have
their patches applied.
# 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.
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
Guy Recommends: Solarwinds' Log & Event Management Tool
LEM will alert you to problems such as when a key
application on a particular server is unavailable. It can also
detect when services have stopped, or if there is a
network latency problem. Perhaps this log and event management
tool's most interesting ability is to take corrective action, for
example by
restarting services, or isolating the source of a maleware attack.
Yet perhaps the killer reason why people
use LEM is for its
compliance capability, with a little help from you, it will ensure that your organization complies with industry
standards such as CISP or FERPA. LEM is a really smart
application that can make correlations between data in different logs,
then use its built-in logic to take corrective action, to restart services,
or thwart potential security breaches.
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
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
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.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.