Windows PowerShell


Start Windows Services with PowerShell

Windows PowerShell Scripting - Start-Service

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 (or more) 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 a 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.  One extra thing, you need faith in the scripters' maxim: 'Any thing that you can do by clicking in a GUI, you can equal (or exceed) in a PowerShell script'.

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, no need to stop then start the service.

Example 1: How to Start a Windows Service (Alerter)*

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!

* Windows Server 2008 does have an Alerter service, thus you need to choose another service such as PLA (Performance Logs and Alerts)

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

  • Launch PowerShell
  • 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 = "Alerter"
$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.

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

The Main Event - Starting Alerter*

* Windows Server 2008 does have an Alerter service, thus you need to choose another service such as PLA (Performance Logs and Alerts)

# PowerShell cmdlet to start a named service
$srvName = "Alerter"
$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 1:  I prepared this 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:

set-Service Alerter -startuptype manual
start-Service Alerter

Note 2: Observe how the speech marks are slightly different in this script:
"$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.

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

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

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

Example 2: How to Stop a Service (Alerter)

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 Alerter service
$srvName = "Alerter"
$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 1: 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 LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor 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 take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

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

All you really need is a 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 1:  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 2:  There will be variations depending on which operating system you are using.

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  • get-Service  • start-Service  • gwmi win32_service

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

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.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.