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.
PowerShell's Get-Service Topics
• Our Mission• Example 1: Listing all the services on your computer • Example 2: Manipulating the Output
• Example 3: Filtering the Output with 'Where' • Summary of PowerShell's 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. This page's PowerShell scripts 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 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
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).
Instructions:
Pre-requisite: Visit Microsoft's site and download the correct version of PowerShell for your operating system.
1. Launch PowerShell
2. Copy get-service * (into memory) 3. Right-click on the PowerShell symbol 4. Edit --> Paste
5. Press enter to execute the code. 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', or 'Sort-Object' 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
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
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"
# 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'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, for a permanent record, how to out-file.Next step, see how to Start, Stop and Restart-Service.