Windows PowerShell


Windows PowerShell get-Service

Windows PowerShell Scripting with get-Service

Our tasks for Windows services are to list them, to stop them, and especially to start them.  I would like to begin on this page with a thorough grounding in both the PowerShell syntax, and also the properties available to the get-Service command.  Once we have mastered the basics of this verb-noun pair, we will investigate tasks for other members of the 'Service' family; for example, I have another page on start-Service.  To digress into WMI, I have a separate page on get-WmiObject win32_service.

Topics for PowerShell get-Service

 ♣

Our Mission

One key role for a good computer techie is checking, starting, and sometimes stopping the operating system's services.  Indeed, expertise with Windows Services is one discriminator between a real network administrator and a 'paper' MCSE impostor.  The PowerShell scripts on this page will get you underway with my mission to monitor and control which Services should be running on your servers and workstations.

Let us build-up logically.  Firstly, we will list all the services on your computer.  Next we will filter the script's 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.  Finally, we can create an advanced script, which will start or stop named services.

Addendum: There is an alternative to the get-Service cmdlet, and that is to involve WMI and experiment with: get-WmiObject win32_service.

Example 1: Listing all the services on your computer

Scripting Windows 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 how a value changes in the Services 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).

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

  • Launch PowerShell
  • Copy the line of code below (into memory)
  • Right-click on the PowerShell symbol
  • Edit --> Paste
  • Press enter to execute the code.

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: 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: 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' on ServiceType rather than the 'Name' property.

# PowerShell cmdlet to list Windows services
get-Service * |Sort-Object -property ServiceType `
| format-Table name, ServiceType, status, CanStop, -auto

Learning Points

Note 1: The tiny backtick` tells PowerShell that the same command continues on the next line.  Without this symbol (`) 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.

Note  Out-GridView: PowerShell v 2.0 introduces a new cmdlet to control data display.  See more on how to pipe the results into out-GridView.

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

# PowerShell cmdlet to list services that are stopped
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 $_.  These three symbols translate to 'this pipeline'.  In the above 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 criterion: "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".

  ˚

Out-file

If I could digress and hark 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"

# PowerShell cmdlet to save services that are stopped to a file
get-Service * |where {$_.Status -eq "Stopped"} `
| out-file "D:\PowerShell\Scripts\services.txt"

Note: As you can see, most of the command is taken up with the path which specifies 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 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, for a permanent record, how to out-file.  Next step:  Start, Stop and Restart-Service.

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.