Ezine 199 – PowerShell Set-Service

Ezine 199 – PowerShell Manages Windows Services

This week I have multiple objectives.  My main aim is to persuade people to follow my lead – abandon VBScript and take-up PowerShell.  My secondary objective is to remind you that PowerShell has more verbs than just ‘Get’; in this ezine we will employ, Set, Start, Stop and Restart.  One minor learning point, once you seek more active commands than ‘get’, you will need to click ‘Run as Administrator’ before you launch PowerShell ISE.

Topics for PowerShell Set-Service


This Week’s Secret

As you may already know, I have a phobia of anti-virus software; whilst troubleshooting a colleague’s machine I could not turn off the AVG program.  Then I remembered Windows Services.  Lo-and-behold, this program had installed itself as a service, and was with great joy that I used a PowerShell command to disable it.  I could have used the operating system’s services GUI, but scripting was a calming antidote to my anger with a tricky problem. 

This Week’s Mission To Control Windows Services with PowerShell

My main mission this week is to lure people away from VBScript and tempt them to use PowerShell instead.  Once I decided to abandon VBScript and take-up PowerShell I have never looked back.  However, I admit that I was never a VB or ASP developer, just a dabbler in VBScript for logon scripts and related tasks.

As a vehicle for our mission I have chosen Windows Services because they are particularly suitable for my secondary agenda, which is to introduce more PowerShell verbs, for example, set, start, stop or restart-service.

A Simple Script to List a Computer’s Services

# Also try removing the # on the next line
# Get-Service -ComputerName YourOtherMachine

Note 1: You can check the status, and startup values, for a named services thus:
get-Service Spooler.

Research Properties of Get-Service by Appending | Get-Member

get-Service  | get-Member -memberType Property

Real-life Lessons from Windows Services

It may seem that we are just bumbling about with these Windows Services, however, if we are observant then we can gather useful knowledge, for example get-Service reveals that Window 7 no longer has the Alerter or Messenger services.  Also try: get-Service WinRm, it will check whether Windows Remoting has been installed. 

What is even more instructive is the relationship between the properties ‘DisplayName’ and ‘Name’.  My point is that when it comes to scripting you need to pay special attention of different values, for example try this:
get-service PRINT spooler |ft name, DisplayName.  Learn from my mistake, I thought its name was Print Spooler, and its description was Spooler.  This was doubly wrong.  The name is ‘spooler’, and for once there is no description property!

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 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 now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Find More PowerShell Verbs to Compliment Noun Service

Once you discover a PowerShell noun such as service, it’s interesting to research which verbs are associated with it, I use this format: get-command -noun service.  Incidentally, PowerShell nouns are invariably singular, hence service works, but not serviceS.

get-Command -noun Service

See more on Get-Service

Adjust the Startup Value with Set-Service

The point of this script is that you cannot start a service whose ‘StartupType’ is disabled, but you can use PowerShell to change it’s value to ‘Automatic’.  It would be a trivial task to then add a line saying: start-Service $CompSrv.

# PowerShell Set-Service Example
$CompSrv ="Spooler"
Set-Service $CompSrv -StartupType Automatic

Note 2: Error Message When Using ‘Set’ or ‘Start’

This problem occurs when we become complacent after using dozens of PowerShell ‘Get’ cmdlets.  Suddenly it throws an error when you use a cmdlet starting with: Start, Stop or even Get.

Start-Service : Service ‘Print Spooler (spooler)’ cannot be started due to the following error: Cannot open spooler service on computer ‘.’.

Normally I praise PowerShell’s error message, but in this case an extra message saying, ‘You need administrative privileges’, would have saved me angst.  Once I understood the problem, the solution was simple, right-click the PowerShell ISE program and select, ‘Run as administrator’.

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

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Stopping and Starting Services with PowerShell

Let us persuade Set-Service to start, or to stop a Windows service.  Incidentally, I like to check the results by launching the Windows Services GUI, I also make frequent use of F5 to refresh the display after running a PowerShell command.

# Introduce a variable to highlight the service’s name
$CompSrv ="Spooler"
Set-Service $CompSrv -Status "Running"

Other values for -Status are "Paused" or "Stopped".  However, I find that dependencies often get in the way of this method, this is why I prefer the sister commands, stop-Service or start-Service.  What I like about this pair is that they support the -force parameter.  The syntax is straightforward, stop-Service Spooler -force.

Restart a Windows Service

$SrvName = "Spooler"
Restart-Service $SrvName
$ServiceAfter = get-service $SrvName
"$SrvName is now " + $ServiceAfter.status

Learning Points

Note 3: You can create and initialize a PowerShell simply by using the dollar sign, for example $SrvName =.

Note 4: Observe how we can display properties with the dot command, for example .status.

Note 5: PowerShell’s (+) sign is versatile, not only is it a math operator, but as here, can also concatenate text.

Note 6: Clear-Host is an unnecessary indulgence, it just removes traces of all my other failed experiments, which could confuse the output.  See more on Restart-Service

Error Message With These Scripts

Start-Service : Service ‘Print Spooler (spooler)’ cannot be started due to the following error: Cannot open spooler service on computer ‘.’.

Normally I praise PowerShell’s error message, but in this case an extra message saying ‘You need administrative privileges’ would have saved me angst.  Once I understood the problem, the solution was simple, right-click the PowerShell ISE program and select, ‘Run as administrator’.

Summary of PowerShell Set-Service

This ezine covers diverse aspects of Windows services.  Firstly, the ‘Get’ verb lists those services installed and displays their status.  Secondly, you can manipulate a named service with ‘Set’.  Thirdly, remember there are 3 more verbs for specific tasks, namely, Stop, Start and Restart.


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.

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   • 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.