Windows PowerShell


Windows PowerShell get-Service

Windows PowerShell Scripting with get-Service

Our mission for Windows services is to list them, to stop them, and especially to start them.  I would like to begin on this page 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, we will investigate tasks for other members of the 'Service' family.  For example, I have another page on start-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".   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.

Sometimes - like now, it's hard for me to stay focussed on the one item, namely scripting with PowerShell.  Instead I get distracted by checking the list of services in case 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

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]*.

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.

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 also more process and service topics

PowerShell Home  • get-Process  • stop-Process  • get-Service  • start-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

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

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

Please report a broken link, or an error.