Ezine 147 – PowerShell Services

Windows PowerShell Scripting – Get-Service

Our PowerShell mission for Windows services is to list them, to stop them, and especially to start them.  I would like to begin this week with a thorough grounding both in the PowerShell syntax, and also the properties available to the get-Service command.  Once we have mastered the basics of this verb-noun pair, then next week we will investigate tasks for other members of the ‘Service’ family of commands.

PowerShell’s Get-Service Topics


This Week’s Secret

Scripting services lends itself to one of my favorite techniques, namely having the GUI open while I execute the code.  The advantage of this approach is two-fold; you can check what the script is doing by observing a value change in the Service GUI.  Also, the GUI’s menus and columns give me ideas for creating better scripts.  If you like this technique, click on the Windows Start button, Run, type services.msc  (do remember the .msc extension). 

One key role for a good computer techie is checking, starting and sometimes, stopping an operating system’s service.  Indeed, expertise with Windows Services is one discriminator between a real network administrator and a ‘paper’ MCSE impostor.  This week’s PowerShell scripts will get you started with my mission to monitor and control which Windows Services should be running on your servers and workstations.

This Week’s Mission

Let us build-up logically.  Firstly, we will list all the services on your computer.  Next we will filter the scripts output so that it lists only services which are "Stopped", or "Running".   From there we will set about adjusting the start-up type to manual, automatic or disabled.  And then next week we will create advanced scripts, which will start or stop named services.

Sometimes – like now, it’s hard for me to stay focussed on the one item namely scripting with PowerShell.  Instead I get distracted checking the list of services to see if any rogue maleware or grayware services have crept onto my computer.  Then I have another run-through the list to see if services that should be disabled, are in fact running.  However, the good news is that while this sidetracks me from writing code, I am increasing my list of useful jobs to automate with PowerShell.

Example 1: Listing all the services on your computer

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.

# clear-Host
 get-service *

Learning Points

Note 1:  It is said that PowerShell is a self-describing language, what this means is that you can interrogate the properties of an object directly.  Don’t miss a chance to experiment with my ‘Trusty Twosome’ of help and get-member.  In this instance try: 

a) help get-Service -full
b) get-Service | get-Member
c) get-Service alerter | get-Member -memberType properties

Note 2: I have yet to find a PowerShell noun that is not singular, for example, Service (and not Services).  Realization of this consistency saves me typos.

Note 3: PowerShell commands are not case sensitive.  Get-service works as well as get-Service.

Challenge:   Try filtering with: get-Service S*  or use the square brackets and select a range:
get-service [r-w]*.

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 2: Manipulating the Output

Following research from get-Service | get-Member, we can refine the output so that we add extra properties, for example CanStop and ServiceType.  This example also illustrates how we can ‘Sort’, or ‘Sort-Object’ on ServiceType rather than the ‘Name’ property.

# clear-Host
get-Service * |Sort-Object -property ServiceType `
| format-table name, ServiceType, status, CanStop, -auto

Learning Points

Note 1: The backtick ` tells PowerShell that the same command continues on the next line.  Without the tiny backtick (`) there is no word-wrap, and thus PowerShell would interpret the new line as a new command.  In this instance we want one long command, which happens to spill over two lines.

Note 2: The -auto parameter produces more sensible column widths.

Note 3: When learning, I like to give the full syntax, however, for production scripts Sort-Object is normally abbreviated to plain ‘Sort’, just as format-table is usually written as ‘ft’.  In fact, you can also omit the -property parameter and the script will still work:

get-Service * |Sort ServiceType |ft name, servicetype, status, canstop, -auto

Challenge 1: Research other properties, in particular more members of the Can* family.

Challenge 2: Try an alternative sort criterion, for example Sort-Object Status.

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 3: Filtering the Output with ‘Where’

In a production network, I see numerous opportunities for a short script to filter which services are running and which services have stopped.  From a scripting point of view, this is a job for a ‘Where’ clause.


# clear-Host
get-Service * |where {$_.Status -eq "Stopped"}

Learning Points

Note 1:  The ‘Where’ command crops up in many PowerShell scripts, therefore it is worth familiarizing yourself with the rhythm of the command. 

Begin with a pipe ‘|’.  Next open {Braces (not parenthesis)}, pay close attention to the $_. these three symbols translate to ‘this pipeline’.  In this example, I have chosen to filter the services based on the value of the property ‘Status’.  Remember that PowerShell uses -eq and not the equals sign.  Finally we have the criteria "Stopped", an alternative filter would be "Running".

Challenge: Try changing "Stopped" to "Running".

For your notebook: In other PowerShell scripts the structure of the ‘Where’ statement would be the same, however, you would replace .Status with a property to suit your purpose.  Each property has its own values, which you would research then substitute for "Stopped" in the above script.


Harking back to the verbosity of VBScript; because it took so much code to list the services, I feared that it would confuse beginners if I added another 10 lines of VBScript in order to output the list of services to a text file.  With PowerShell there is no such worry, all you need to store the results is to append these few words:
| out-file "D:\PowerShell\Scripts\services.txt"

As you can see, most of the command is taken up with the path to where you want to create the file, naturally you must amend this path to a location on your computer.  Remember to add out-file to your notebook of invaluable PowerShell commands.

Summary of Windows PowerShell’s Get-Service

I declare that this page well and truly covers the basics of get-Service.  The examples explain how to list all the services, how to filter with a ‘Where’ clause, and finally how to out-file for a permanent record.  Next week it will be Start, Stop and Restart-Service.

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


See More Windows PowerShell Examples of Real-life Tasks

PowerShell Tutorials  • PowerShell Examples  • IpConfig  • Get-Counter  • PowerShell NetSh

Monitor Performance – PowerShell  • PowerShell temp   • PowerShell Delete Temporary files

PowerShell WOL (Wake-on-Lan)  • Services   • Change Computer Description Registry

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.