Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



PowerShell Restart-Service

Windows PowerShell Restart-Service

The most efficient way for PowerShell to cure a hung service is with the verb restart.  Restart-Service xyz is just simpler and more efficient than stop-Service xyz, followed by start-Service xyz.  If you are new to PowerShell Restart-Service, then I suggest that you begin with my Get-Service page.

Topics for PowerShell Restart-Service

 ♣

Example 1: How to Restart a Service

The service which benefits most from Restart-Service is, "Spooler".  The reason being the printer gives more trouble than any other piece of hardware, and often restarting the Spooler cures the problem.  The inferior, but ruthless method of curing such printer jams is to reboot the computer.  When the the computer is also a fileserver, this technique is undesirable.

Production Script

All you really need is this simple command:

# PowerShell Restart-Service example
Restart-Service "Spooler"

Note 1: You can change "Spooler" to the name of another service.  To list services see PowerShell's Get-Service

Comprehensive Restart-Service Script

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

Learning Points

Note 2:  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 3:  There will be variations depending on which operating system you are using.

Guy Recommends: A Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

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. 

Download your free copy of Wake-On-LAN

Example 2: Avoid the Trap of Name or DisplayName

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 the 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 use the name 'Print Spooler', you need to specify the property -DisplayName.

# PowerShell Restart-Service example using displayName
Restart-Service -displayName "Print Spooler"

Note 4:  You could research the names and displayNames with Get-Service.

Example 3: How to Simply 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 it's been removed from Windows 7 and Server 2008 does I have choosen PLA (Performance Logs and Alerts) to test PowerShell's service cmdlets.

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 5:  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 6:  Observe how I mix the ''literal phrases'' with the $variables and properties to produce a meaningful output.

Guy Recommends:  Solarwinds' Log & Event Management ToolSolarwinds Log and 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.

Download your FREE trial of Solarwinds Log & Event Management tool.

The Main Event - Starting PLA

 

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.

# PowerShell cmdlet to start a named service
$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 7:  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 8:  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:

set-Service PLA -startuptype manual
start-Service PLA

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

Solarwinds' Orion 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.

Perhaps the NPM's best feature is the way it suggests solutions to network problems.  Its second best feature is 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 take advantage of Solarwinds' offer.

Download a free trial of the Network Performance Monitor.

Example 4: 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 Plug & Play 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 9: 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.

 

The Service Family (Each member has a different verb)

Clear-Host
Get-Command -noun 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.

Remoting with PowerShell Service

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

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.

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  • Get-WmiObject win32_service

Get-Service  • Start-Service  • Stop-Service  • Restart-Service  • Set-Service

PowerShell Start-Sleep   • 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

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

 

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

Please report a broken link, or an error.