PowerShell Basics: Start, Stop & Restart a Service with Examples

PowerShell Basics_ Start, Stop & Restart a Service

Windows PowerShell Start-Service Cmdlet

Our mission on this page is start a named Windows service.  If necessary, we can modify the script to stop, or even 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 Start-Service Topics

Our Mission

Our mission is to start one of your operating system’s services.  We can also adapt the script to stop services, but that is less exciting.  It is also worth mentioning that another member of this family is called Restart-Service.

The result of my preliminary experiment reveals that it’s not possible to start a service whose start-up type is currently set to, ‘Disabled’.  Good news; a walk-through with the Services GUI reveals that if you switch a service from Disabled to Manual, then you can start it.  Incidentally, I believe in the maxim: ‘Any thing that you can do by clicking in a GUI, you can equal (or exceed) in a PowerShell script’.

Example 1: How to Start a Windows Service

In Windows Server 2003 days I choose the Alerter service for testing, partly because it’s relatively harmless service, and partly because its name is near the top of the list!  However, since Alerter has been removed from Windows 7 and Server 2008, I have chosen PLA (Performance Logs and Alerts) to test PowerShell’s service cmdlets.

Instructions:
Pre-requisite: Visit Microsoft’s site and download the correct version of PowerShell for your operating system.

  • Launch PowerShell, the ISE GUI would be best.
  • Copy the four lines of code below (into memory)
  • Right-click on the PowerShell symbol
  • Edit –> Paste
  • Press enter to execute the code.

Preliminary Script

Let us check the service’s status, and also let us ‘warm up’ with Get-Service before we employ other members of the service family.

# PowerShell cmdlet to check a service's status
$srvName = "PLA"
$servicePrior = Get-Service $srvName
$srvName + " is now " + $servicePrior.status

Learning Points

Note 1: I have decided to introduce the variable $srvName to hold the value of the service.  Hopefully this will emphasise the name of the service, and prompt you to change it as necessary for your project.

Note 2: Observe how I mix the ”literal phrases” with the $variables and properties to produce a meaningful output.

The Main Event – Starting Service PLA (Performance Logs and Alerts)

Operating system younger than Windows 7 and Server 2008 do not have an Alerter service, this is why I now use PLA (Performance Logs and Alerts) to test PowerShell’s Start-Service.

Check Status – And Start Service

# PowerShell cmdlet to start a named service
Clear-Host
$srvName = "PLA"
$servicePrior = Get-Service $srvName
"$srvName is now " + $servicePrior.status
Set-Service $srvName -startuptype manual
Start-Service $srvName
$serviceAfter = Get-Service $srvName
"$srvName is now " + $serviceAfter.status

Learning Points

Note 3: Observe how the speech marks are slightly different in this script compared with the previous:
“$srvName is now ” + $servicePrior.status
compared with
$srvName + ” is now ” + $servicePrior.status

My points are:
a) Experiment yourself.
b) To some extent, these learning scripts leave traces of my thinking process.

Note 4: I prepared the above script to help you appreciate the factors needed to control a Windows Service.  It also reflects my thinking process of how I learn about a command.  On the other hand, for a production script you could take a much more ruthless approach and simplify the script thus:

Simple Script to Start the Service

# PowerShell set, then start a service
Clear-Host
Set-Service PLA -startuptype manual
Start-Service PLA

Note 5: See more on Set-Service

Guy Recommends:  Network Performance Monitor (FREE TRIAL)Review of Orion NPM v11.5

SolarWinds Network Performance Monitor (NPM) 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 on a 30-day free trial.

SolarWinds Network Performance Monitor Download 30-day FREE Trial

Example 2: How to Stop a Service

In real life, you may want a script which ensures that services such as: Telnet, Messenger and Routing and Remote Access are Stopped.  Just for testing, you may need a script which reverses Start-Service, just to be sure that it really is working as designed.  Either way, here is a script which stops the service defined by $srvName.

# PowerShell cmdlet to stop the Perf log and alert service
$srvName = "PLA"
$servicePrior = Get-Service $srvName
"$srvName is now " + $servicePrior.status
Stop-Service $srvName
$serviceAfter = Get-Service $srvName
Set-Service $srvName -startuptype disabled
"$srvName is now " + $serviceAfter.status

Learning Points

Note 6: Observe how this script is the mirror image of the Start-Service script.  It even disables the service once it has stopped.  If you remember, when Example 1 wanted to start a service, it must first make sure the -startuptype is set to manual.

In Order to Explain a Trap – I Digress:

What’s in a Name?  What do these groups of services have in common?

Group A
Alerter, Messenger, WebClient

Group B
Print Spooler, Telnet, Telephony and Windows Time

More importantly, why won’t PowerShell’s service family interact with Group B?

The Answer: Some services have a ‘Display Name’ which differs from their ‘Service Name’, for example Telnet and Tlnsvr.  How did I find this out?  When I tried to start ‘Telnet’, or ‘Print Spooler’, nothing happened.  Yet if I had a manual walk-through in the Service GUI, no problem.  Then I ran Get-Service * and observed the two columns, Name and also Display Name.  What threw me into confusion was Group A, where both names are the same.

Just to emphasise, if you wish to control ‘Print Spooler’, you need to script its Name – ‘Spooler’.  If you double-check with the command: Get-Service s* you see Name: Spooler, Display Name: ‘Print Spooler’.

Guy Recommends: SolarWinds Engineer’s Toolset (FREE TRIAL)Engineer's Toolset v10

This Engineer’s Toolset 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 on a 14-day free trial now!

SolarWinds Engineer's Toolset Download 14-day FREE Trial

Example 3: How to Restart a Service (Spooler)

A classic service to practice the Restart-Service command is, “Spooler”.  One reason for choosing this particular service is that the printer gives more trouble than any other piece of hardware, and sometimes Restarting the Spooler cures the problem.  The inferior, but ruthless method of curing such printer jams is to reboot the computer.  However, if the computer is also a server, this method is undesirable.

The real-life situation of a jammed printer spooler is not straightforward.  The point is that it APPEARS to be running, but in fact, it’s not working.  The smartest solution is to Restart the service.  As with previous examples, when you are learning, open the services.msc GUI and experiment with the settings.  What you will discover is that you can also Restart a service that has stopped.

Production Script

To achieve a restart, all you really need is this one-liner:

Restart-Service "Spooler"

Learning Script

# PowerShell cmdlet to Restart the Spooler service
$srvName = "Spooler"
$servicePrior = Get-Service $srvName
"$srvName is now " + $servicePrior.status
Set-Service $srvName -startuptype manual
Restart-Service $srvName
$serviceAfter = Get-Service $srvName
"$srvName is now " + $serviceAfter.status

Learning Points

Note 7: My biggest fear is that in a production script I will misspell the name of the service.  Thus, check for success by observing this system message:

WARNING: Waiting for service ‘Print Spooler (Spooler)’ to finish starting…

Note 8: There will be variations depending on which operating system you are using.

Guy Recommends: SolarWinds Wake-On-LAN Utility (FREE TOOL)Solarwinds Wake-On-LAN

Encouraging computers to sleep when they’re not in use is a great idea – until you are away from your desk and need a file on that remote sleeping machine!

WOL also has business uses, for example, rousing machines so that they can have update patches applied.  My real reason for recommending you download this free tool is because it’s so much fun sending those ‘Magic Packets’. Give WOL a try – it’s free.

SolarWinds Wake-On-LAN Download 100% FREE Tool

PowerShell 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-Object { $_.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

More Research on PowerShell’s Get-Service

Every time I run a check of cmdlet’s capabilities with Get-Help I discover a new parameter, or useful example.  In this case, I am reminded of the -DisplayName and -DependentServices parameters.  Try the command for yourself.

Clear-Host
Get-Help Get-Service -full

The Service Family (Each member has a different verb)

Talking of research, I also like to know more about ‘sister’ commands; for example, Restart-Service is much neater than using Stop-Service, then Start-Service.

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.

See also a function I created called Get-ServiceStatus »

Summary of PowerShell’s Start-Service

If your mission is to master the Start-Service command, commence with Get-Service.  Once you have mastered the rhythm of the Get-Service Verb-Noun pair, move on to the Start, Stop, and Restart-Service family of PowerShell commands.  For scripting purposes, make sure that you use the true Service Name, and avoid the Service’s Display Name.  A final piece of advice, open the Service GUI so that you can double-check that what your script is doing is what you intended.


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.