Ezine 148 Start Windows Services with PowerShell

Windows PowerShell Scripting – Get-Service

On the work front, nothing major happens in August.  Even if you are not on holiday, everyone else who matters seems to be.  September on the other hand, has a feeling of back to work, a time for new initiatives.  In the case of this ezine, September is a great time to trumpet my goal for everyone to learn PowerShell. 

Let me start by saying that there are several reasons that I send this newsletter.  Firstly, to keep in touch with those who buy my ebooks and visit my site, secondly, to send out free updates, and thirdly, to encourage you to learn PowerShell.  If none of these apply do unsubscribe, or write to me and I will remove you from the mailing list.

PowerShell’s Start-Service Topics


This Week’s Secret

Following on from my theme of back to work, my not so secret mission is to persuade anyone who uses a computer regularly to experiment with Windows PowerShell.  Perhaps you already know DOS?  There may have been times when this command line language has saved the day.  PowerShell literally does everything that DOS does, but caters for that moment when you wished DOS had more wide-ranging capabilities. 

I really am not trying to sell you anything, it’s just that I have this vision of PowerShell being the smart way to configure or interrogate computers, and I want you to keep one step ahead of those dinosaurs who stick with DOS commands.  Also, perhaps you have always wanted to learn a scripting language such as VBScript or C++.  If so then why not try PowerShell, it is easy to get started, challenging for intermediates, incredibly productive for experts.

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 worth mentioning that another member of this family is called restart-Service.  Incidentally, this mission follows on from the previous ezine where we just listed the services.

The result of my preliminary experiment reveals that it’s not possible to start a service whose start-up type is ‘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.

Research for yourself with this one-liner
get-Command *service

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

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!

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

  • Launch PowerShell
  • Copy the three 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

# 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 in controlling 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, my scripts leave traces of my thinking process.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

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, first, it must 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 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 the Name ‘Spooler’.  If you double-check with the command: get-Service s* you see Name: Spooler, Display Name: ‘Print Spooler’.

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…

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 command, 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.

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


See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell ‘where‘  • PowerShell ‘Sort’

Windows PowerShell Modules  • Import-Module  • PowerShell Module Directory 

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links.